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));
  • does the product with id 7 exist ? – Reena Parekh Dec 19 '16 at 7:43
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.

| improve this answer | |
  • Thanks, but it still does not work. I have got the same result. – Roland Soós Apr 25 '16 at 13:32

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.