cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 : Add to cart programmatically conflicting with Full Page Cache

Magento 2 : Add to cart programmatically conflicting with Full Page Cache

I want add to cart programmatically and it is working fine. But when Magento Full page Cache is enabled, then it is not working. Here is my Code.

<?php
$id = 6377;
$qty = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$cart = $objectManager->create('Magento\Checkout\Model\Cart');
$http = $objectManager->create('Magento\Framework\App\Response\Http');
$session = $objectManager->get('Magento\Checkout\Model\Session');
$cart->addProduct($product,array('qty' => $qty));
$cart->save();
$session->setCartWasUpdated(true);
$http->setRedirect('checkout/cart/index')->sendResponse();
exit;
?>
2 REPLIES 2

Re: Magento 2 : Add to cart programmatically conflicting with Full Page Cache

hi

for version 2.1.16 in mode developer is OK

 

best regards

Re: Magento 2 : Add to cart programmatically conflicting with Full Page Cache

I have same problem