2

Is there any way to update the mini cart view?

Now I'm using:

      require([
            'Magento_Customer/js/customer-data'
        ], function (customerData) {

            alert("hola");
            var sections = ['cart'];
            customerData.invalidate(sections);
        });

But it only refreshes the view by updating the page.

I'm looking for ways to make the changes reflect without having to reload the page. Any ideas?

3 Answers 3

20

You can try the below code.

require([
           'Magento_Customer/js/customer-data'
        ], function (customerData) {
            var sections = ['cart'];
            customerData.invalidate(sections);
            customerData.reload(sections, true);
        });
3
  • when i recharge the page show "You added Pantalon vaquero to your shopping cart." could remove the message for no show?
    – Miguel
    Commented Jun 9, 2017 at 9:52
  • NOt working for me.I am removing an item from minicart it is not removing checkout button.
    – akgola
    Commented Oct 19, 2018 at 4:45
  • Error: Uncaught TypeError: Cannot read property 'invalidate' of undefined Commented Aug 23, 2021 at 12:07
4

Faced the same issue, used single line in ajax success to update mini cart.

require('Magento_Customer/js/customer-data').reload();

Full Ajax code:

<script type="text/javascript">    
    require([ 'jquery', 'jquery/ui'], function($){ 
        $('#ck-quick-qosubmit').click(function(){
            $.ajax({
                    url:"/quickadd/quick",
                    method:"POST",

                    data:{
                       sku: $('#ck-quick-sku').val(),
                       qty: $('#ck-quick-qty').val(),
                    },
                    success:function(response) {
                        $('#ck-quickbuy-response').html(response);
                        require('Magento_Customer/js/customer-data').reload();
                   },
                   error:function(){
                     console.log('error');
                   }

              });

        }); 
    });     

</script>

Hope this will help someone in future :)

2

The right way of doing this (usually it's enough) is adding your action to section.xml. See example in Magento_Checkout:

<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>

Actually after that magento will do the same as @suresh-chikani showed on ajaxComplete event

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.