productRepository = $productRepository; } /** * Initialize product instance from request data * * @return \Magento\Catalog\Model\Product|false */ protected function _initProduct() { $productId = (int)$this->getRequest()->getParam('product'); if ($productId) { $storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId(); try { return $this->productRepository->getById($productId, false, $storeId); } catch (NoSuchEntityException $e) { return false; } } return false; } /** * Add product to shopping cart action * * @return \Magento\Framework\Controller\Result\Redirect * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute() { $result = []; if (!$this->_formKeyValidator->validate($this->getRequest())) { return $this->resultRedirectFactory->create()->setPath('*/*/'); } $params = $this->getRequest()->getParams(); try { if (isset($params['qty'])) { $filter = new \Zend_Filter_LocalizedToNormalized( ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()] ); $params['qty'] = $filter->filter($params['qty']); } $product = $this->_initProduct(); $related = $this->getRequest()->getParam('related_product'); /** * Check product availability */ if (!$product) { return $this->goBack(); } $this->cart->addProduct($product, $params); 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->cart->getQuote()->getHasError()) { $message = __( 'You added %1 to your shopping cart.', $product->getName() ); $this->messageManager->addSuccessMessage($message); $result['success'] = true; $result['messages'] = $message; if (isset($params['isCheckoutPage'])){ $_layout = $this->_objectManager->get('Magento\Framework\View\LayoutInterface'); $_layout->getUpdate()->load([ 'cartquickpro_checkout_cart_index', 'checkout_cart_item_renderers','checkout_item_price_renderers']); $_layout->generateXml(); $_output = $_layout->getOutput(); $result['content'] = $_output; $result['isPageCheckoutContent'] = true; } } } } catch (\Magento\Framework\Exception\LocalizedException $e) { if ($this->_checkoutSession->getUseNotice(true)) { $this->messageManager->addNotice( $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($e->getMessage()) ); $result['messages'] = $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($e->getMessage()); $result['success'] = true; } else { $messages = array_unique(explode("\n", $e->getMessage())); foreach ($messages as $message) { $this->messageManager->addError( $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($message) ); } $result['messages'] = join(', ', $messages); $result['success'] = false; } $storeManager = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface'); $baseUrl= $storeManager->getStore()->getBaseUrl(); $url = $baseUrl.'cartquickpro/catalog_product/options/id/'.$params['product']; if (!$url) { $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl(); $url = $this->_redirect->getRedirectUrl($cartUrl); } $result['url'] = $url; } catch (\Exception $e) { $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.')); $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e); $result['messages'] = __('We can\'t add this item to your shopping cart right now.'); $result['success'] = false; } $result['isAddToCartBtn'] = (!isset($params['isCheckoutPage']) && $this->cart->getItemsCount()) ? true : false ; return $this->_jsonResponse($result); } protected function _jsonResponse($result) { return $this->getResponse()->representJson( $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result) ); } /** * Resolve response * * @param string $backUrl * @param \Magento\Catalog\Model\Product $product * @return $this|\Magento\Framework\Controller\Result\Redirect */ protected function goBack($backUrl = null, $product = null) { if (!$this->getRequest()->isAjax()) { return parent::_goBack($backUrl); } $result = []; if ($backUrl || $backUrl = $this->getBackUrl()) { $result['backUrl'] = $backUrl; } else { if ($product && !$product->getIsSalable()) { $result['product'] = [ 'statusText' => __('Out of stock') ]; } } $this->getResponse()->representJson( $this->_objectManager->get('Magento\Framework\Json\Helper\Data')->jsonEncode($result) ); } }