1

I am looping some specific products on the home page but unable to generate the ADD TO CART URL in magento 2

How to generate ADD TO CART URL when displaying products in loop.

5 Answers 5

9

I know it's been a year since this has been touched on, but as I've just done what has been requested, I thought that I may post my solution to help others.

The other answers will work, but only for simple products, where no further input is required (e.g. selecting options). When using getAddToCartUrl(), Magento first checks if the products requires any options to be selected; if it does, then it will simply supply the URL to the product page instead.

To skip all of these checks and directly obtain the add to cart URL, then make use of the Magento\Checkout\Helper\Cart helper instead. If you are within a .phtml file, then this can be utilised simply by calling $this->helper:

$cartHelper = $this->helper('Magento\Checkout\Helper\Cart');

From there, you can generate the add to cart URL via getAddUrl(), ensuring you pass the product object as the parameter:

echo $cartHelper->getAddUrl($product)

For it to fully work, you must have a hidden field for the form key as described in the other answers, and if the product in question has compulsory options to choose from (e.g. a configurable product), then make sure you include those fields as well, otherwise you will get redirected to the product page itself, with a message informing the user that options are required.

2
  • I like this suggestion, because using Magento\Checkout\Helper\Cart can also be done in a Controller or a Model, not only in a Block or template. This should be the accepted answer IMO. Commented Aug 7, 2018 at 9:32
  • I'm not sure I understand this solution. How do you define $product? Commented Nov 9, 2018 at 21:23
3

The excellent solution is to use

$cartHelper = $this->helper('Magento\Checkout\Helper\Cart');

and after that

echo $cartHelper->getAddUrl($product);

this give add to cart URL every time (for a simple product, for a simple product with custom options etc)

2

use following to generate add to cart URL in magento2:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');

$addToCartUrl =  $listBlock->getAddToCartUrl($product);
5
  • It generates the URL but when i click on this link it takes me to the CART with not item. (You have no items in your shopping cart.) Commented Jun 15, 2016 at 13:56
  • Ye me too I shall look for soltion now Commented Aug 21, 2016 at 12:18
  • Did you find any solution for this ? Commented Sep 23, 2016 at 15:19
  • Object Manager shouldn't be used - magento.stackexchange.com/questions/117098/…
    – user3608589
    Commented Nov 7, 2016 at 16:46
  • On list page this code redirects to product view page for configurable products and works for simple products.
    – shashank
    Commented Mar 13, 2018 at 12:48
2

Add your button code into form with form key.. It will work

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');

$addToCartUrl =  $listBlock->getAddToCartUrl($product);

<form data-role="tocart-form" action="<?php echo $addToCartUrl; ?>" method="post">
    <?php echo $block->getBlockHtml('formkey')?>
       <button type="submit"
               title="Add to Cart"
               class="action tocart primary">
               <span>Add to Cart</span>
        </button>
 </form>
1
  • Don't use object manager directly
    – Cypher909
    Commented Dec 8, 2021 at 20:27
0

You can generate "add to cart" url by following code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl =  $listBlock->getAddToCartUrl($_product);

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.