0

I want to redirect to cart page after adding the product into cart, I know there is a provision in config but I have a custom product type and that config is not working for that product.

That configuration setting is working for simple and other products but not for the custom product type.

So Is there any way I can do this thing programmatically using plugins or anything else.

2 Answers 2

1
  1. app/code/Vendor/Module/etc/frontend/di.xml

    <?xml version="1.0"?>
    <config>
        <type name="Magento\Checkout\Controller\Cart\Add">
            <plugin name="interceptAddProductToCartRedirect" type="Vendor\Module\Plugin\Checkout\Cart\Add" sortOrder="1" disabled="false"/>
        </type>
    </config>
    
  2. app/code/Vendor/Module/Plugin/Checkout/Cart/Add.php

    <?php
    
    namespace Vendor\Module\Plugin\Checkout\Cart;
    
    use Magento\Framework\UrlInterface;
    use Magento\Framework\App\Request\Http;
    use Psr\Log\LoggerInterface;
    
    class Add
    {
        protected $url;
        protected $request;
        protected $logger;
    
        public function __construct(
            UrlInterface $url,
            Http $request,
            LoggerInterface $logger
        )
        {
            $this->_url = $url;
            $this->_request = $request;
            $this->_logger = $logger;
        }
    
        /**
         * Redirect to checkout/cart after Adding product to cart
         *
         * @param \Magento\Checkout\Controller\Cart\Add $subject
         * @param \Closure $proceed
         * @return mixed
         */
        public function aroundExecute(\Magento\Checkout\Controller\Cart\Add $subject, \Closure $proceed)
        {
            $result = $proceed();
            $this->_logger->info("Add to cart plugin OK");
            if (!$this->_request->isAjax()) {
                $result->setUrl($this->_url->getUrl('checkout/cart'));
                return $result;
            }
            return $result;
        }
    }
    
  3. app/code/Vendor/Module/view/frontend/layout/catalog_product_view.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceBlock name="product.info.addtocart">
                <action method="setTemplate">
                    <argument name="template" xsi:type="string">Vendor_Module::product/view/addtocart.phtml</argument>
                </action>
            </referenceBlock>
        </body>
    </page>
    
  4. copy addtocart.phtml from :

    vendor\magento\module-catalog\view\frontend\templates\product\view\addtocart.phtml

    into :

    app/code/Vendor/Module/view/frontend/templates/product\view\addtocart.phtml

    then add "bindSubmit": true like this :

    <script type="text/x-magento-init">
    {
        "#product_addtocart_form": {
            "Magento_Catalog/js/validate-product": {
                "bindSubmit": true, <!-- HERE -->
            }
        }
    }
    

  5. generate DI configuration & flush cache storage

    php bin/magento s:d:c
    php bin/magento c:f
    

    Good luck.

1

please refere the below links. This may help you.

https://magento-qa.com/how-can-i-redirect-to-the-cart-page-programmatically

How can I redirect to the cart page programmatically?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.