Today we are discussing how to update mini cart after adding product in cart.
In magento 2 when we add a product programmatically then mini cart is not updated automatically, we have to update it by the code.
Firstly, we have to write code for adding product in cart programmatically.
Here is a link in which we discuss about how to add product programmatically.
In the above blog we are using controller named: Index controller with AddProducttocart action.
now for the cart updation we have to create a file at path:
Add following code in this file.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
Here, in section.xml we have to define,
action name: here in action name we define modulename_controller_action name, after which we want to update cart.
section name: is which section you want to update, here we want to update cart section.
and these sections are realated to customer that’s why we define Magento_Customer:etc/sections.xsd in the config tag.
After adding this file your cart will automatically get update after hello/index/addProducttocart controller execution.
Hope, it will help you.