0

i have create my custom add to cart controller my cart update but mini cart not update this is my code

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();    
   $request = $objectManager->get('\Magento\Framework\App\Request\Http'); 
   $resultJsonFactory = $objectManager->get('\Magento\Framework\Controller\Result\JsonFactory'); 
   $resultJson = $resultJsonFactory->create();
   $product_id = $request->getParam('product_id');
   $product_qty = $request->getParam('product_qty');

    $id = $product_id;
    $qty = $product_qty;
    $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();

    if($session->setCartWasUpdated(true))
    {
        $response="success";
    }
    else
    {
        $response = "fail";
    }
    $http->setRedirect('checkout/cart/index')->sendResponse();
    return $resultJson->setData(['success' => $response]);
Hitesh Koshti
  • 1,445
  • 4
  • 18
  • 36
test
  • 21
  • 5

1 Answers1

0

Create sections.xml file inside your Module:

Path: [Vendor][Modul]\etc\frontend\sections.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="[Modul]/cartaction/add">
        <section name="cart"/>
        <section name="messages"/>
    </action>
</config>

Action Name : Your custom addtocart controller.

Renga
  • 371
  • 3
  • 10