How to Update Mini Cart in Magento2 ?
Update mini cart when you are submitting a form
Magento provides a way to update minicart if you are submitting a form. First of all create sections.xml file in ‘etc/frontend’ folder in your module.
Now write following code snippet in sections.xml file
<?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="modulename/controller/addProduct"> <section name="cart"/> </action> </config>
Action name: action name is controller where you are submitting form.
Example of this situation may be when you want to set custom price of product in your own controller. This approach will work when you are using POST or PUT form submitting. It will also work for AJAX request.
If you still have any query regarding this “How to” or would like to add some suggestions to this solution, let us know your feedback or query at [email protected], and don’t forget to share this “How to” blog with your fellow Magento 2 users!