passing product id and form key through url but product not added to cart in magento2.
  • How you pass this? Write more. – Sohel Rana Apr 1 '19 at 14:05
  • Generated the cart url and added the parameters (product id and form_key) but product is not added to cart – venkata prasad Apr 1 '19 at 14:07

Magento 2.3 has stopped adding product to cart via GET request. It will only add product to cart if you pass your parameters like product id and other needed parameters in POST. If you are using GET method, it will simply redirect with error message.

As a workaround,you can create your custom module. Add following 2 files to your custom module.


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <router id="standard">
        <route id="customaddtocart" frontName="customaddtocart">
            <module name="[Vendor]_[Module]"/>



namespace [Vendor]\[Module]\Controller\Cart;

class AddAction extends \Magento\Framework\App\Action\Action
    protected $storeManager;
    protected $cart;
    protected $productRepository;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Catalog\Model\ProductRepository $productRepository
        $this->storeManager = $storeManager;
        $this->cart = $cart;
        $this->productRepository = $productRepository;

    public function execute()
        $product_id = $this->getRequest()->getParam('product');
        $product = $this->getProductById($product_id);
        $params = [];
        $params['product'] = $product->getId();
        $params['form_key'] = $this->getRequest()->getParam('form_key');
        $params['qty'] = 1;
        $this->cart->addProduct($product, $params);

        // You can either redirect to some page or return json response from here

    public function getProductById($id)
        return $this->productRepository->getById($id, false, $this->storeManager->getStore()->getId());

Now you can call your controller like, http://yourwebsite.com/customaddtocart/cart/add/product/1/form_key/xxxxx

  • is there any alternative to add product to cart through url yash shah – venkata prasad Apr 2 '19 at 4:58
  • You have to create your custom controller and add product to cart via coding and call that controller. Let me know if you need a sample code. – Yash Shah Apr 2 '19 at 5:02
  • yes can you share the sample code module – venkata prasad Apr 2 '19 at 5:07
  • Edited the answer with sample code, Have not tested the code but it should work. Also replace [Vendor] and [Module] with your vendor and module name where needed. – Yash Shah Apr 2 '19 at 5:22
  • it is required di.xml file right to override the add.php – venkata prasad Apr 2 '19 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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