![]() 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/Cnc/Checkout/Preference/Controller/Cart/ |
<?php /** * Copyright (c) 2020 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Cnc * Krzysztof Majkowski <[email protected]> */ namespace Cnc\Checkout\Preference\Controller\Cart; use Cnc\Catalog\Model\Attribute\MsiAttributes; use Cnc\Checkout\Api\GetAssignedSourceCodesInterface; use Cnc\Checkout\Helper\Cart; use Ecombricks\InventoryInventorySales\Api\GetProductSalableQtyInterface; use Exception; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Catalog\Model\Product; use Magento\Checkout\Model\Cart as CustomerCart; use Magento\Checkout\Model\Session; use Magento\Framework\App\Action\Context; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Data\Form\FormKey\Validator; use Magento\Framework\Escaper; use Magento\Framework\Exception\InputException; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Locale\ResolverInterface; use Magento\Quote\Model\Quote\Item; use Magento\Store\Model\ScopeInterface; use Magento\Store\Model\StoreManagerInterface; use Psr\Log\LoggerInterface; use Zend_Filter_LocalizedToNormalized; class Add extends \Magento\Checkout\Controller\Cart\Add { /** * @var GetProductSalableQtyInterface */ private $getProductSalableQty; /** * @var GetAssignedSourceCodesInterface */ private $getAssignedSourceCodes; /** * @var MsiAttributes */ private $msiAttributes; /** * @var array */ private $sourcesByWebsite = []; /** * @var Cart */ private $cartHelper; /** * Add constructor. * @param Context $context * @param ScopeConfigInterface $scopeConfig * @param Session $checkoutSession * @param StoreManagerInterface $storeManager * @param Validator $formKeyValidator * @param CustomerCart $cart * @param ProductRepositoryInterface $productRepository * @param GetProductSalableQtyInterface $getProductSalableQty * @param GetAssignedSourceCodesInterface $getAssignedSourceCodes * @param MsiAttributes $msiAttributes * @param Cart $cartHelper */ public function __construct( Context $context, ScopeConfigInterface $scopeConfig, Session $checkoutSession, StoreManagerInterface $storeManager, Validator $formKeyValidator, CustomerCart $cart, ProductRepositoryInterface $productRepository, GetProductSalableQtyInterface $getProductSalableQty, GetAssignedSourceCodesInterface $getAssignedSourceCodes, MsiAttributes $msiAttributes, Cart $cartHelper ) { parent::__construct( $context, $scopeConfig, $checkoutSession, $storeManager, $formKeyValidator, $cart, $productRepository ); $this->getProductSalableQty = $getProductSalableQty; $this->getAssignedSourceCodes = $getAssignedSourceCodes; $this->sourcesByWebsite = []; $this->msiAttributes = $msiAttributes; $this->cartHelper = $cartHelper; } public function execute() { if (!$this->_formKeyValidator->validate($this->getRequest())) { $this->messageManager->addErrorMessage( __('Your session has expired') ); return $this->resultRedirectFactory->create()->setPath('*/*/'); } $params = $this->getRequest()->getParams(); try { if (isset($params['qty'])) { $filter = new Zend_Filter_LocalizedToNormalized( ['locale' => $this->_objectManager->get( ResolverInterface::class )->getLocale()] ); $params['qty'] = $filter->filter($params['qty']); } $product = $this->_initProduct(); $related = $this->getRequest()->getParam('related_product'); /** Check product availability */ if (!$product) { return $this->goBack(); } /** * KDC MODIFICATION START * * Modified due to split between sources. */ if (isset($params['super_group'])) { foreach ($params['super_group'] as $productId => $qty) { if ($qty > 0) { $simpleProduct = $this->getProduct($productId); if ($simpleProduct) { $this->addProduct($simpleProduct, $qty); } } } } else { $this->addProduct($product, $params['qty']); } /** KDC MODIFICATION END */ if (!empty($related)) { $this->cart->addProductsByIds(explode(',', $related)); } $this->cart->save(); /** * @todo remove wishlist observer \Magento\Wishlist\Observer\AddToCart */ $this->_eventManager->dispatch( 'checkout_cart_add_product_complete', ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()] ); if (!$this->_checkoutSession->getNoCartRedirect(true)) { if ($this->shouldRedirectToCart()) { $message = __( 'You added %1 to your shopping cart.', $product->getName() ); $this->messageManager->addSuccessMessage($message); } else { $this->messageManager->addComplexSuccessMessage( 'addCartSuccessMessage', [ 'product_name' => $product->getName(), 'cart_url' => $this->getCartUrl(), ] ); } if ($this->cart->getQuote()->getHasError()) { $errors = $this->cart->getQuote()->getErrors(); foreach ($errors as $error) { $this->messageManager->addErrorMessage($error->getText()); } } return $this->goBack(null, $product); } } catch (LocalizedException $e) { if ($this->_checkoutSession->getUseNotice(true)) { $this->messageManager->addNoticeMessage( $this->_objectManager->get(Escaper::class)->escapeHtml($e->getMessage()) ); } else { $messages = array_unique(explode("\n", $e->getMessage())); foreach ($messages as $message) { $this->messageManager->addErrorMessage( $this->_objectManager->get(Escaper::class)->escapeHtml($message) ); } } $url = $this->_checkoutSession->getRedirectUrl(true); if (!$url) { $url = $this->_redirect->getRedirectUrl($this->getCartUrl()); } return $this->goBack($url); } catch (Exception $e) { $this->messageManager->addExceptionMessage( $e, __('We can\'t add this item to your shopping cart right now.') ); $this->_objectManager->get(LoggerInterface::class)->critical($e); return $this->goBack(); } return $this->getResponse(); } /** * Add product to cart. * * @param Product $product * @param int $qty * @throws LocalizedException */ protected function addProduct(Product $product, int $qty) { $sourceData = $this->cartHelper->getSourceDataForProduct($product, $this->cart->getItems()); if ($qty > $sourceData['max']) { throw new LocalizedException(__('The requested qty is not available')); } $sources = $this->cartHelper->getSourcePriority(); $currentSources = $sourceData['sources']; $params = $this->getRequest()->getParams(); foreach ($sources as $sourceCode) { if (isset($currentSources[$sourceCode]) && $currentSources[$sourceCode] > 0) { $mainProduct = $this->_initProduct(); $this->setSourceCode($sourceCode); $this->_view->getLayout()->setSourceCode($sourceCode); $add = min($qty, $currentSources[$sourceCode]); $qty -= $add; $params['source'] = $sourceCode; if (isset($params['super_group'])) { $params['super_group'][$product->getId()] = (int) $add; } else { $params['qty'] = $add; } $this->getRequest()->setParams($params); $this->cart->addProduct($mainProduct, $params); if ($qty <= 0) { break; } } } } /** * Initialize product instance from request data * * @return Product|false */ protected function _initProduct() { $productId = (int)$this->getRequest()->getParam('product'); if ($productId) { try { $storeId = $this->_objectManager->get( StoreManagerInterface::class )->getStore()->getId(); /** * KDC MODIFICATION START * * Modified due to force product reload to be able to split product between sources. */ return $this->productRepository->getById($productId, false, $storeId, true); /** KDC MODIFICATION END */ } catch (NoSuchEntityException $e) { return false; } } return false; } /** * @param int $productId * @return false|ProductInterface */ protected function getProduct(int $productId) { if ($productId) { try { $storeId = $this->_objectManager->get( StoreManagerInterface::class )->getStore()->getId(); return $this->productRepository->getById($productId, false, $storeId, true); } catch (NoSuchEntityException $e) { return false; } } return false; } /** * Returns cart url * * @return string */ private function getCartUrl() { return $this->_url->getUrl('checkout/cart', ['_secure' => true]); } /** * Is redirect should be performed after the product was added to cart. * * @return bool */ private function shouldRedirectToCart() { return $this->_scopeConfig->isSetFlag( 'checkout/cart/redirect_to_cart', ScopeInterface::SCOPE_STORE ); } }