8

We need to update the main cart on change of quantity by Ajax without click on "Update Cart Button" and without reloading the page. Below my code in this code only update cart subtotal but not updated cart page summery sidebar subtotal.

 require(['jquery', 'Magento_Customer/js/customer-data',
    'jquery/jquery-storageapi'], function ($) {
    // $("#submitbutton").hide();
    var form = $('form#form-validate');
    var qtyfields = $('input.qty');
    $('.page.messages').each(function () {
        var thismessage = $(this);
        thismessage.attr('id', 'messages');
    });

    form.find(qtyfields).each(function (e) {
        var thisfield = $(this);
        $(this).change(function () {
            console.log('change detected');
            form.submit();
        });

    });
    $(document).on('submit','form#form-validate', function (e) {
        var thisfield = $(this);
        var thisiteminfo = thisfield.closest(".item-info");
        var thissubtotal = thisiteminfo.children(".subtotal");
        var formsubtotals = $(this).find('.subtotal');
        e.preventDefault();
        $.ajax({
            url: form.attr('action'),
            data: form.serialize(),
            type: 'post',
            success: function (res) {
                var parsedResponse = $.parseHTML(res);
                var result = $(parsedResponse).find("#form-validate");
                $("#form-validate").replaceWith(result);
                var totals = $(parsedResponse).find("#cart-totals");
                $("#cart-totals").replaceWith(totals);
                console.log(totals);
                //location.reload();
            },
            error: function () {
                console.log('error');
            }
        });
        console.log('form submitted');
    });
}); 

Please help me for find best solution.

  • You can log parsedResponse? – Mohamed El Mrabet Nov 27 '17 at 9:02
  • Yes, cart quantity updated correctly but cart subtotal and cat page summery section subtotal not updated. – Prathap Madhavan Nov 27 '17 at 11:34
  • Did you find a solution? – Bram Mar 27 '18 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.