2

I have generated an add to cart url on the frontend, but it do not seem to work. When I click on the link, I get the "You have no items in your shopping cart." page.

I have got this add to cart url from Magento: http://test.com/magento2/checkout/cart/add/uenc/aHR0cDovL2xvY2FsaG9zdC9tYWdlbnRvMi8,/product/7/

And here is the code how I created the url:

$id = 7; 

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

$productHelper = $om->get('\Magento\Catalog\Model\Product');

$cartHelper = $om->get('\Magento\Checkout\Helper\Cart');

echo $cartHelper->getAddUrl($productHelper->load($id));
1
  • does the product with id 7 exist ? Dec 19, 2016 at 7:43

1 Answer 1

1

Use below code:

$id = 7; 

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

$productHelper = $om->get('\Magento\Catalog\Model\Product');

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

echo $listBlock->getAddToCartUrl($productHelper->load($id));

It helpful to you.

1
  • Thanks, but it still does not work. I have got the same result. Apr 25, 2016 at 13:32

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.