![]() 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\ProductFactory; use Magento\Framework\Controller\Result\JsonFactory; use Magento\Framework\Serialize\Serializer\Json; class Save extends \Magento\Framework\App\Action\Action { protected $messageManager; protected $_responseFactory; protected $_url; protected $_request; protected $cartsession; protected $modelsavecart; protected $modelsavecartdetail; protected $_modelCart; protected $serialize; protected $quoteFactory; protected $product; protected $_customersession; protected $jsonFactory; public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\App\ResponseFactory $responseFactory, \Magento\Customer\Model\Session $customer_session, Session $session, SavecartproFactory $modelsavecart, SavecartprodetailFactory $modelsavecartdetail, Cart $modelCart, QuoteFactory $quoteFactory, ProductFactory $product, JsonFactory $jsonFactory, Json $serialize ) { $this->_responseFactory = $responseFactory; $this->cartsession = $session; $this->modelsavecart = $modelsavecart; $this->modelsavecartdetail = $modelsavecartdetail; $this->_modelCart = $modelCart; $this->quoteFactory = $quoteFactory; $this->product = $product; $this->_customersession=$customer_session; $this->jsonFactory=$jsonFactory; $this->serialize = $serialize; parent::__construct($context); } public function execute() { try { $data = $this->_request->getParams(); $quote = $this->cartsession->getQuote(); $customer_id = $quote->getCustomerId(); $cartname = $data['cartname']; if($cartname!="") { if ($customer_id != null) { // Quote Save To Main Tabel $savecartmodel = $this->modelsavecart->create() ->setCartName($cartname) ->setCustomerId($customer_id) ->setCreatedAt(date('Y-m-d H:i:s')) ->save(); // Quote Data Save To Detail Tabel $visitems = $this->cartsession->getQuote()->getAllVisibleItems(); foreach ($visitems as $item) { foreach ($item->getOptions() as $option) { /* KDC modification start */ if ($option['value'] && $this->isJson($option['value'])) { $itemOptions = $this->serialize->unserialize($option['value']); $this->modelsavecartdetail->create() ->setSavecartId($savecartmodel->getId()) ->setQuotePrdId($item->getProductId()) ->setQuotePrdQty($item->getQty()) ->setQuotePrdPrice($item->getPrice()) ->setQuoteConfigPrdData($this->serialize->serialize($itemOptions)) ->save(); break; } /* KDC modification end */ //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(); $this->_modelCart->removeItem($itemId)->save(); } $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; } else { $message = "Before saving cart you must have login"; $this->messageManager->addError($message); $accUrl = $this->_url->getUrl('customer/account/login'); $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } else{ $message = "Cart name not blank"; $this->messageManager->addError($message); $response = array(); $resultJson = $this->jsonFactory->create(); $response['redirectUrl'] = $this->_url->getUrl('checkout/cart/index'); } } catch(\Exception $e) { $this->messageManager->addError($e->getMessage()); $accUrl = $this->_url->getUrl('savecart/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 */ }