Source of file CompareListItemModel.php
Size: 6,377 Bytes - Last Modified: 2021-12-23T10:52:54+00:00
/var/www/docs.ssmods.com/process/src/src/Model/CompareListItemModel.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | <?php namespace Toast\ShopAPI\Model; use Exception; use Omnipay\Common\Currency; use SilverShop\Model\OrderItem; use SilverShop\Model\Variation\Variation; use SilverShop\Page\Product; use SilverStripe\Control\Controller; use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config; use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\ArrayList; use SilverStripe\View\ArrayData; use SilverStripe\Dev\Debug; /** * Class CartItemModel */ class CompareListItemModel extends ProductModel { /** @var Product|Variation $item */ protected $item; protected $toggle_link; protected $item_id; protected $product_id; protected static $fields = [ 'id', 'title', 'link', 'price', 'price_nice', 'sku', 'add_link', 'product_image', 'categories', 'variations', 'item_id', 'product_id', 'toggle_link', ]; public function __construct($id) { /** ========================================= * @var Currency $unitMoney * @var Currency $totalMoney * ========================================*/ parent::__construct($id); if ($id && is_numeric($id)) { // Get an order item $this->item = Product::get_by_id(Product::class, $id); if ($this->item) { // Set the initial properties $this->item_id = $this->item->ID; $this->product_id = $this->item->ID; $this->title = $this->item->Title; $this->link = $this->item->AbsoluteLink(); $this->endpoint = Controller::join_links(Director::absoluteBaseURL(), 'shop-api/compare', $this->item->ID); $this->toggle_link = Controller::join_links($this->endpoint, 'toggle'); } } $this->extend('onAfterSetup'); } public function addOrRemoveItems() { $this->called_method = 'toggle'; $request = Injector::inst()->get(HTTPRequest::class); $session = $request->getSession(); $compareList = $session->get('compareList'); if ($this->item) { // check if item already in wishlist if (!$compareList){ $session->set('compareList', []); $compareList = $session->get('compareList'); } // if already exists remove it if (in_array($this->item->ID, $compareList)){ $key = array_search ($this->item->ID, $compareList); unset($compareList[$key]); $this->code = 200; $this->status = 'success'; $this->message = _t('SHOP_API_MESSAGES.CompareListItemRemoved', 'Item removed from the compare list successfully.'); $this->refresh = [ 'comparelist' ]; }else{ $compareList[] = $this->item->ID; $this->code = 200; $this->status = 'success'; $this->message = _t('SHOP_API_MESSAGES.CompareListItemAdded', 'Item added to compare list successfully.'); $this->refresh = [ 'comparelist' ]; } $compareList = array_unique($compareList); $session->set('compareList', $compareList); } else { $this->code = 404; $this->status = 'error'; $this->message = _t('SHOP_API_MESSAGES.CompareListItemNotFound', 'Item does not exist in compare list'); } $this->extend('onAddOrRemoveItems'); return $this->getActionResponse(); } public function addOrRemoveVariations() { $this->called_method = 'toggle'; $request = Injector::inst()->get(HTTPRequest::class); $session = $request->getSession(); $id = $request->param('ID'); $compareList = $session->get('compareList_variations'); $this->item = Variation::get_by_id(Variation::class, $id); if ($this->item) { // Set the initial properties $this->item_id = $this->item->ID; $this->product_id = $this->item->ID; $this->title = $this->item->Title; $this->link = $this->item->AbsoluteLink(); $this->endpoint = Controller::join_links(Director::absoluteBaseURL(), 'shop-api/compare', $this->item->ID); $this->toggle_link = Controller::join_links($this->endpoint, 'toggle'); } if ($this->item) { // check if item already in wishlist if (!$compareList){ $session->set('compareList_variations', []); $compareList = $session->get('compareList_variations'); } // if already exists remove it if (in_array($this->item->ID, $compareList)){ $key = array_search ($this->item->ID, $compareList); unset($compareList[$key]); $this->code = 200; $this->status = 'success'; $this->message = _t('SHOP_API_MESSAGES.CompareListItemRemoved', 'Item removed from the compare list successfully.'); $this->refresh = [ 'compareList_variations' ]; }else{ $compareList[] = $this->item->ID; $this->code = 200; $this->status = 'success'; $this->message = _t('SHOP_API_MESSAGES.CompareListItemAdded', 'Item added to compare list successfully.'); $this->refresh = [ 'compareList_variations' ]; } $compareList = array_unique($compareList); $session->set('compareList_variations', $compareList); } else { $this->code = 404; $this->status = 'error'; $this->message = _t('SHOP_API_MESSAGES.CompareListItemNotFound', 'Item does not exist in compare list'); } $this->extend('onAddOrRemoveVariations'); return $this->getActionResponse(); } } |