![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Magecomp/Savecartpro/Controller/Index/ |
<?php namespace Magecomp\Savecartpro\Controller\Index; use Magento\Checkout\Model\Session; use Magecomp\Savecartpro\Model\SavecartproFactory; use Magecomp\Savecartpro\Model\SavecartprodetailFactory; use Magento\Checkout\Model\Cart; use Magento\Quote\Model\QuoteFactory; use Magento\Catalog\Model\Product; use Magento\Setup\Exception; use Magento\Framework\Controller\Result\JsonFactory; class Update extends \Magento\Framework\App\Action\Action{ protected $messageManager; protected $_responseFactory; protected $_url; protected $_request; protected $cartsession; protected $modelsavecart; protected $modelsavecartdetail; protected $_modelCart; protected $quoteFactory; protected $product; protected $jsonFactory; protected $serialize; public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\ResponseFactory $responseFactory, Session $session, SavecartproFactory $modelsavecart, SavecartprodetailFactory $modelsavecartdetail, Cart $modelCart, QuoteFactory $quoteFactory, Product $product, JsonFactory $jsonFactory, \Magento\Framework\Serialize\Serializer\Json $serialize) { $this->_responseFactory = $responseFactory; $this->cartsession = $session; $this->modelsavecart = $modelsavecart; $this->modelsavecartdetail = $modelsavecartdetail; $this->_modelCart = $modelCart; $this->quoteFactory=$quoteFactory; $this->product=$product; $this->jsonFactory=$jsonFactory; $this->serialize = $serialize; parent::__construct($context); } public function execute() { try { $data = $this->_request->getParams(); $quote = $this->_modelCart->getQuote(); $cartid = $data['savecartid']; $quotedetailCollection = $this->modelsavecartdetail->create() ->getCollection() ->addFieldToFilter('savecart_id',$cartid); // Quote Data Save To Detail Tabel $quoteid=$quote->getId(); $quote = $this->quoteFactory->create()->load($quoteid); $visitems = $quote->getAllVisibleItems(); foreach ($visitems as $item) { $flag=false; if(isset($item)) { foreach ($item->getOptions() as $option) { try { /* KDC modification start */ if ($option['value'] && $this->isJson($option['value'])) { $itemOptions = $this->serialize->unserialize($option['value']); break; } /* KDC modification end */ } catch (Exception $e) { $this->messageManager->addError($e->getMessage()); $accUrl = $this->_url->getUrl('savecartpro/index/view'); $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } } foreach ($quotedetailCollection as $cartitem) { $objItem = $cartitem; $currentQty = $cartitem->getQuotePrdQty(); $Configdata = $this->serialize->unserialize($cartitem->getQuoteConfigPrdData()); if ($cartitem->getQuotePrdId() == $item->getProductId()) { $product = $this->product->load($cartitem->getQuotePrdId()); if($product->getTypeId()=="simple"){ if(isset($itemOptions['options'])) { $customopt1 = $itemOptions['options']; $customopt2 = $Configdata['options']; } } if($product->getTypeId()=="configurable"){ if(isset($itemOptions['super_attribute'])) { $customopt1 = $itemOptions['super_attribute']; $customopt2 = $Configdata['super_attribute']; } } if($product->getTypeId()=="bundle"){ if(isset($itemOptions['bundle_option'])) { $customopt1 = $itemOptions['bundle_option']; $customopt2 = $Configdata['bundle_option']; } } if(isset($customopt1) and isset($customopt2)) { if(is_array($customopt1) and is_array($customopt2)) { $array_cmp = array_diff_assoc($customopt1, $customopt2); if (count($array_cmp) == 0) { $flag = true; break; } } } else { if($cartitem->getQuotePrdId() == $item->getProductId()){ $flag=true; break; } } } } if ($flag) { $finaQty=$item->getQty() + $currentQty; $Configdata['qty']=$finaQty; $objItem->setQuotePrdQty($finaQty) ->setQuoteConfigPrdData($this->serialize->serialize($Configdata)) ->save(); } else { foreach ($item->getOptions() as $option) { /* KDC modification start */ if (!$this->isJson($option['value'])) { continue; } /* KDC modification end */ $itemOptions = $this->serialize->unserialize($option['value']); $savecartdetailmodel = $this->modelsavecartdetail->create() ->setSavecartId($cartid) ->setQuotePrdId($item->getProductId()) ->setQuotePrdQty($item->getQty()) ->setQuotePrdPrice($item->getPrice()) ->setQuoteConfigPrdData($this->serialize->serialize($itemOptions)) ->save(); break; //itemOptions contain all the custom option of an item } } } $this->_modelCart->truncate();// Clear Shopping Cart $this->cartsession->clearQuote();// Clear Checkout Session // Clear Shopping Cart $visitems = $this->cartsession->getQuote()->getAllVisibleItems(); foreach($visitems as $item) { $itemId = $item->getItemId(); } $message = "Your cart has been successfully saved."; $this->messageManager->addSuccess($message); $response = array(); $resultJson = $this->jsonFactory->create(); $response['redirectUrl'] = $this->_url->getUrl('savecartpro/customer/cartlist'); $resultJson->setData($response); return $resultJson; } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); $accUrl = $this->_url->getUrl('savecartpro/index/view'); $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } /* KDC modification start */ /** * @param $string * @return bool */ private function isJson($string): bool { json_decode($string); return (json_last_error() == JSON_ERROR_NONE); } /* KDC modification end */ }