1

I have create a custom module and perform the action using ajax call. I have created the sections.xml and update the below code.

<?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="southlincprotection/Index/Index">
        <section name="cart"/>
    </action>
</config>

but it is not working and not updating the minicart item after succesfull ajax execution. can anyone please let me where I am doing mistakes.

Package Name >> Southlinc

Module Name >> Protection

Controller >> Index >> Index.php

I have defined the frontname "southlincprotection" in routes.xml

my Ajax code is below

var param = 'quoteid=<?php echo $quoteId; ?>&ajax=1';
    $.ajax({
        showLoader: true,
        url: '<?php echo $baseUrl; ?>southlincprotection',
        data: param,
        type: "POST",
        dataType: 'json'
    });
5
  • In which page are you going to implement this and what is your front name? Is it southlincprotection?
    – P S
    Jan 12, 2018 at 6:58
  • My frontname is southlincprotection I want to use in my custom action when I tried to execute the Ajax call
    – Jack
    Jan 12, 2018 at 9:10
  • Just try to debug the file which told you I had the same problem I fixed it by debugging :) @Jack
    – Keyur Shah
    Jan 12, 2018 at 9:14
  • I have tried with your solution but not getting success. Thanks
    – Jack
    Jan 12, 2018 at 9:15
  • Not getting success means ? You only need to debug the piece of code nothing else, after debugging you get to know that why your code is not working. @Jack
    – Keyur Shah
    Jan 13, 2018 at 12:28

4 Answers 4

2

The problem could be the use of capital letter in action. Change it with:

<?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="southlincprotection/index/index">
        <section name="cart"/>
    </action>
</config>

Firstly make sure that your template is being run and your js is being loaded

3
  • Thanks, but it didn't work for me
    – Jack
    Jan 12, 2018 at 9:41
  • first load your controller in your browser and check if everything is working fine.
    – P S
    Jan 12, 2018 at 9:43
  • My other things are working fine when I execute my action
    – Jack
    Jan 12, 2018 at 9:51
2

The ajax mini cart updated code below work for, you can try this.

    $.ajax({
                    url: "/southlincprotection/Index/Index",
                    data: {
                        item_id: this.id,
                    },
                    showLoader: true,
                    type: 'POST',
                    dataType: 'json',
                    success: function (response) {
                        console.log("Message : "+response.success);
                        require([
                           'Magento_Customer/js/customer-data',
                           'Magento_Checkout/js/model/quote',
                           'ko',
                           'uiComponent',
                           'jquery',
                           'Magento_Checkout/js/model/cart/totals-processor/default',
                           'Magento_Checkout/js/model/cart/cache'
                        ], function (customerData, quote, ko, Component,$, defaultTotal, cartCache) {
                            'use strict';
                            var sections = ['cart'];
                            customerData.invalidate(sections);
                            customerData.reload(sections, true);
                            defaultTotal.estimateTotals();
                            cartCache.set('totals',null);
                        });
                    }
                });
2

add this to the success or complete callback

  var sections = ['cart'];
   customerData.invalidate(sections);
   customerData.reload(sections, true)

and dont forget to require 'Magento_Customer/js/customer-data'

1
  • This worked for me.
    – TheTC
    Dec 1, 2021 at 20:58
0

The main file responsible to update mini cart is,

Magento\Customer\view\frontend\web\js\customer-data.js

in this file, you can find this piece of code,

$(document).on('ajaxComplete', function (event, xhr, settings) {
    var sections,
        redirects;

    if (settings.type.match(/post|put|delete/i)) {
        sections = sectionConfig.getAffectedSections(settings.url);

        if (sections) {
            customerData.invalidate(sections);
            redirects = ['redirect', 'backUrl'];

            if (_.isObject(xhr.responseJSON) && !_.isEmpty(_.pick(xhr.responseJSON, redirects))) { //eslint-disable-line
                return;
            }
            customerData.reload(sections, true);
        }
    }
});

So, I would suggest debugging only this code in chrome or firefox I am sure you will definitely get the reason.

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.