cancel
Showing results for 
Search instead for 
Did you mean: 

Redirecting to cart with PHP in a module on Magento 2.2

SOLVED
   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Redirecting to cart with PHP in a module on Magento 2.2

I've made a module with the help of this thread that adds a product to the cart from a URL. However, the redirect at the end is a simple redirect and so ignores my store that I've got set in the URL.

 

For example, if I add to cart with the URL /en/addtobasket/product?id=101, it currently redirects to /checkout/cart/, but it should redirect to /en/checkout/cart/.

 

What code do I need instead of $this->getResponse()->setRedirect('/checkout/cart/') to redirect to the current store's cart URL?

 

Full module code is here:

 

<?php
namespace Orbitsound\addtobasket\Controller\Product;
 
class Index extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $cart;
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $product;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Catalog\Model\Product $product,
        \Magento\Checkout\Model\Cart $cart
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->cart = $cart;
        $this->product = $product;
        parent::__construct($context);
    }
    public function execute()
    {
        try {
            $params = array();
            $params['qty'] = '1';//product quantity
            /* Get product id from a URL like /addtobasket/product?id=1,2,3 */
            $pIds = explode(',',$_GET['id']);
            foreach($pIds as $value) {
                $_product = $this->product->load($value);
                if ($_product) {
                    $this->cart->addProduct($_product, $params);
                    $this->cart->save();
                }
            }

            $this->messageManager->addSuccess(__('Add to cart successfully.'));
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addException(
                $e,
                __('%1', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('error.'));
        }
        /*Redirect to cart page*/
        $this->getResponse()->setRedirect('/checkout/cart/');
    }
}

Apologies, this must have been answered a million times before, but I can't seem to find a simple answer to my question.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Redirecting to cart with PHP in a module on Magento 2.2

I just managed to figure it out, I don't know if it's the most efficient way, but it works. I added the below in various places:

 

protected $_storeManager;
\Magento\Store\Model\StoreManagerInterface $storeManager
$this->_storeManager = $storeManager;
$store = $this->_storeManager->getStore();
$this->getResponse()->setRedirect('/' . $store->getCode() . '/checkout/cart/');

 

Which resulted in my end code:

 

<?php
namespace Orbitsound\addtobasket\Controller\Product;
 
class Index extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $cart;
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $product;
    
    protected $_storeManager;   

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Catalog\Model\Product $product,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->cart = $cart;
        $this->product = $product;
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }
    public function execute()
    {
        try {
            $params = array();
            $params['qty'] = '1';//product quantity
            /* Get product id from a URL like /addtobasket/product?id=1,2,3 */
            $pIds = explode(',',$_GET['id']);
            foreach($pIds as $value) {
                $_product = $this->product->load($value);
                if ($_product) {
                    $this->cart->addProduct($_product, $params);
                    $this->cart->save();
                }
            }

            $this->messageManager->addSuccess(__('Add to cart successfully.'));
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addException(
                $e,
                __('%1', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('error.'));
        }
        /*Redirect to cart page*/
        $store = $this->_storeManager->getStore();
        $this->getResponse()->setRedirect('/' . $store->getCode() . '/checkout/cart/');
    }
}

View solution in original post

2 REPLIES 2

Re: Redirecting to cart with PHP in a module on Magento 2.2

Any idea guys? :/

Re: Redirecting to cart with PHP in a module on Magento 2.2

I just managed to figure it out, I don't know if it's the most efficient way, but it works. I added the below in various places:

 

protected $_storeManager;
\Magento\Store\Model\StoreManagerInterface $storeManager
$this->_storeManager = $storeManager;
$store = $this->_storeManager->getStore();
$this->getResponse()->setRedirect('/' . $store->getCode() . '/checkout/cart/');

 

Which resulted in my end code:

 

<?php
namespace Orbitsound\addtobasket\Controller\Product;
 
class Index extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $cart;
    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $product;
    
    protected $_storeManager;   

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Catalog\Model\Product $product,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->resultPageFactory = $resultPageFactory;
        $this->cart = $cart;
        $this->product = $product;
        $this->_storeManager = $storeManager;
        parent::__construct($context);
    }
    public function execute()
    {
        try {
            $params = array();
            $params['qty'] = '1';//product quantity
            /* Get product id from a URL like /addtobasket/product?id=1,2,3 */
            $pIds = explode(',',$_GET['id']);
            foreach($pIds as $value) {
                $_product = $this->product->load($value);
                if ($_product) {
                    $this->cart->addProduct($_product, $params);
                    $this->cart->save();
                }
            }

            $this->messageManager->addSuccess(__('Add to cart successfully.'));
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addException(
                $e,
                __('%1', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('error.'));
        }
        /*Redirect to cart page*/
        $store = $this->_storeManager->getStore();
        $this->getResponse()->setRedirect('/' . $store->getCode() . '/checkout/cart/');
    }
}