Magento – Update Minicart programatically in magento 2

magento2mini-cartupgrade

I have create problematically adding number of products at a time in Magento 2 cart and there I can one see one first product in mini cart other products are also in mini-cart but not showing the count and price

here is the code that i tried to update mini-cart

app/code/Multiple/Addtocart/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Multiple_Addtocart" setup_version="2.1.1">
   </module>
   </config>

app/code/Multiple/Addtocart/view/frontend/layout/default.xml

page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
  <body>
    <referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="refresh.minicart" template="Multiple_Addtocart::refreshMinicart.phtml"  />
    </referenceContainer>
  </body>
</page>

app/code/Multiple/Addtocart/view/frontend/templates/refreshMinicart.phtml

<?php
?>
<script>
  require([
    'Magento_Customer/js/customer-data'
  ], function (customerData) {
    var sections = ['cart'];
    customerData.invalidate(sections);
  });
</script>

But here i'm getting object not created error

enter image description here

Best Answer

Please add sections.xml file under

etc -> fronted -> 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="checkout/cart/add">
       <section name="cart"/>
    </action>