![]() 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/Customer/ |
<?php namespace Magecomp\Savecartpro\Controller\Customer; use Magecomp\Savecartpro\Model\SavecartprodetailFactory; class Custaddcart extends \Magento\Framework\App\Action\Action { protected $messageManager; protected $custsession; protected $_responseFactory; protected $_url; protected $_request; protected $modelsavecartdetail; protected $serialize; protected $productCollectionFactory; public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Customer\Model\Session $custsession, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Framework\App\ResponseFactory $responseFactory, SavecartprodetailFactory $modelsavecartdetail, \Magento\Framework\Serialize\Serializer\Json $serialize) { $this->custsession = $custsession; $this->_responseFactory = $responseFactory; $this->modelsavecartdetail = $modelsavecartdetail; $this->productCollectionFactory = $productCollectionFactory; $this->serialize = $serialize; parent::__construct($context); } public function AddQuoteToCart($data) { try { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $formkey = $objectManager->get('Magento\Framework\Data\Form\FormKey'); $cart = $objectManager->get('Magento\Checkout\Model\Cart'); $quotedetailCollection = $this->modelsavecartdetail->create() ->getCollection() ->addFieldToFilter('savecart_id', $data['cart_id']); foreach ($quotedetailCollection->getData() as $col) { $obj_product = $objectManager->create('Magento\Catalog\Model\Product'); $Configdata = $this->serialize->unserialize($col['quote_config_prd_data']); $Configdata['qty'] = $col['quote_prd_qty']; array_push($Configdata, 'form_key', $formkey->getFormKey()); $_product = $obj_product->load($col['quote_prd_id']); $cart->addProduct($_product, $Configdata); } $cart->save(); return 1; } catch (\Exception $e) { return $e->getMessage(); } } public function execute() { if ($this->custsession->isLoggedIn()) { try { $data = $this->_request->getParams(); if ($data['submit_cart_action'] == 'add_cart') // Customer Want To Added Product To Cart { $result = $this->AddQuoteToCart($data); if ($result) { $message = "Your saved cart has been successfully added to cart"; $this->messageManager->addSuccess($message); $accUrl = $this->_url->getUrl('checkout/cart/'); $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } else { $this->messageManager->addError($result); $accUrl = $this->_url->getUrl('savecartpro/customer/viewcart') . 'qid/' . $data['cart_id']; $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } if ($data['submit_cart_action'] == 'update_cart') // Customer Want To Update Quort Data { $om = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $om->get('\Magento\CatalogInventory\Api\StockStateInterface'); foreach ($data['cartitems'] as $key => $val) { $quotedetail = $this->modelsavecartdetail->create()->load($key); $configdata = $this->serialize->unserialize($quotedetail->getQuoteConfigPrdData()); $configdata['qty'] = $val; if(isset($configdata['selected_configurable_option'])) //configration product { $product = $om->create('Magento\Catalog\Model\Product')->load($configdata['selected_configurable_option']); $configproduct = $om->create('Magento\Catalog\Model\Product')->load($configdata['product']); $qty = $StockState->getStockQty($configdata['selected_configurable_option'], $product->getStore()->getWebsiteId()); if ($configdata['qty'] > $qty) { $message = "We don't have as many " . $configproduct->getName() . " as you requested."; $this->messageManager->addError($message); $accUrl = $this->_url->getUrl('savecartpro/customer/viewcart') . 'qid/' . $data['cart_id']; $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); return; } $quotedetail->setQuoteConfigPrdData($this->serialize->serialize($configdata)); $quotedetail->setQuotePrdQty($val); $quotedetail->save(); break; } // echo"<pre>";print_r($configdata);exit; if($configdata['super_product_config']['product_type'] === 'grouped') //grouped product { // $keys1 = array(); $quotedetailCollection = $this->modelsavecartdetail->create() ->getCollection() ->addFieldToFilter('savecart_id', $data['cart_id']); $count = count($quotedetailCollection->getData()); for($i=0; $i < $count; $i++){ $keys = array_keys($data['cartitems']); $quotedetailCollection->addFieldToFilter('savecart_detail_id',$keys[$i]); $test = $quotedetailCollection->getData(); $value = $data['cartitems'][$keys[$i]]; $qty = $StockState->getStockQty($test[0]['quote_prd_id']); $quotedetail = $this->modelsavecartdetail->create()->load($keys[$i]); if ($value > $qty) { $message = "We don't have as many " . $configdata['super_product_config']['product_type'] . " as you requested."; $this->messageManager->addError($message); $accUrl = $this->_url->getUrl('savecartpro/customer/viewcart') . 'qid/' . $data['cart_id']; $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); return; } $quotedetail->setQuoteConfigPrdData($this->serialize->serialize($configdata)); $quotedetail->setQuotePrdQty($value); $quotedetail->save(); } break; } $product = $om->create('Magento\Catalog\Model\Product')->load( $configdata['product']); $qty=$StockState->getStockQty($configdata['product'],$product->getStore()->getWebsiteId()); if($configdata['qty'] > $qty) { $message = "We don't have as many ".$product->getName()." as you requested."; $this->messageManager->addError($message); $accUrl = $this->_url->getUrl('savecartpro/customer/viewcart') . 'qid/' . $data['cart_id']; $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); return; } $quotedetail->setQuoteConfigPrdData($this->serialize->serialize($configdata)); $quotedetail->setQuotePrdQty($val); $quotedetail->save(); } $message = "Your cart has been successfully updated."; $this->messageManager->addSuccess($message); $accUrl = $this->_url->getUrl('savecartpro/customer/viewcart') . 'qid/' . $data['cart_id']; $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); $accUrl = $this->_url->getUrl('savecartpro/customer/viewcart') . 'qid/' . $data['cart_id']; $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } else { $accUrl = $this->_url->getUrl('customer/account/login'); $this->_responseFactory->create()->setRedirect($accUrl)->sendResponse(); $this->setRefererUrl($accUrl); } } }