1

When the add to cart button is clicked, the page is redirected to the home page, and I would like to stay on the current page. I have tried different ways but couldn't accomplish it or getting an error:

public function execute()
    {
        if (!$this->ajaxHelper->isEnabled()) {
            return parent::execute();
        }

        if (!$this->formKeyValidator->validate($this->getRequest())) {
            return $this->resultRedirectFactory->create()->setPath('catalog/product/', ['_current' => true])
        }

        $params = $this->getRequest()->getParams();
        try {
            if (array_key_exists('qty', $params)) {
                $filter = $this->localizedToNormalized;
                $params['qty'] = $filter->filter($params['qty']);
            }

            $product = $this->initProduct();

            /**
             * Check product availability
             */
            if (!$product) {
                return $this->resultRedirectFactory->create()->setPath('/');
            }

            $data = [
                'status' => true,
                'added' => false,
                'messages' => []
            ];

            $result = $this->dataObjectFactory->create()->setData($data);

            $this->_eventManager->dispatch(
                'magepow_ajaxcart_add_before',
                ['product' => $product, 'request' => $this->getRequest(), 'result' => $result]
            );

            if (!$result->getData('status') && empty($messages)) {
                return $this->resultRedirectFactory->create()->setPath('/');
            }

            $this->processAddProduct($result, $product, $params);
            $this->cart->save();

            $this->_eventManager->dispatch(
                'checkout_cart_add_product_complete',
                ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
            );

            $resultItem = $product->getTypeId() == \Magento\GroupedProduct\Model\Product\Type\Grouped::TYPE_CODE ?
                                $this->dataObjectFactory->create()->setProduct($product)
                                : $this->registry->registry('last_added_quote_item');
            return $this->returnResult($resultItem, $this->relatedAdded);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addNoticeMessage(
                $this->escaper->escapeHtml($e->getMessage())
            );

            $result = [];
            $productId = $this->getProductId();
            $result['error'] = true;
            $result['error_info'] = $e->getMessage();
            $result['id'] = $productId;
            $result['url'] = $this->escaper->escapeUrl(
                $this->urlInterface->getUrl('ajaxcart/index/view', ['id' => $productId])
            );
            $result['view'] = true;
            // $qty = isset($params['qty']) ? $params['qty'] : 1;
            // $stockQty = $product->getExtensionAttributes()->getStockItem()->getQty();
            if( ($product->getTypeId() == 'simple' && !$product->getData('has_options')) || $this->getRequest()->getPost('related_product') ) {
                $result['view'] = false;
            }
            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($result);
            return $resultJson;
        } catch (\Exception $e) {
            $this->messageManager->addExceptionMessage(
                $e,
                __('We can\'t add this item to your shopping cart right now.')
            );
            $this->loggerInterface->critical($e);

            $result = [];
            $result['error'] = true;

            $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
            $resultJson->setData($result);
            return $resultJson;
        }
    }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.