0

I want to display messages on the cart page. I have made a custom module so that the shopping cart will automatically reload when the qty changes using AJAX.

Here is my js code:

define([
'jquery',
'Magento_Checkout/js/action/get-totals',
'Magento_Customer/js/customer-data',
'domReady!'
], function (,ドル getTotalsAction, customerData) {
$(document).on('change', 'input[name$="[qty]"]', function(){
 var form = $('form#form-validate');
 $.ajax({
 url: form.attr('action'),
 data: form.serialize(),
 showLoader: true,
 success: function (res) {
 var parsedResponse = $.parseHTML(res);
 var result = $(parsedResponse).find("#form-validate");
 $("#form-validate").replaceWith(result);
 //reload minicart
 var sections = ['cart'];
 customerData.invalidate(sections);
 customerData.reload(sections, true);
 //reload total summary
 var deferred = $.Deferred();
 getTotalsAction([], deferred);
 
 },
 error: function (xhr, status, error) {
 var err = eval("(" + xhr.responseText + ")");
 console.log(err.Message);
 }
 });
});
});

If I update qty in cart so that the qty is higher than the quantity available. I want to display a message like "The requested qty is not available"?

Can I use customerData to show error messages and display them on the page?

Thanks.

asked Sep 3, 2020 at 21:59

2 Answers 2

0
answered Sep 6, 2020 at 9:42
0

Finally was able to fix issue. I just had to reload the message after setting CustomerData message section.

//Display error if found after jquery
 var messages = $.cookieStorage.get('mage-messages');
 if (!_.isEmpty(messages)) {
 customerData.set('messages', {messages: messages});
 $.cookieStorage.set('mage-messages', '');
 }
 //reload messages section
 var messages_section = ['messages'];
 //customerData.invalidate(messages_sections);
 customerData.reload(messages_section);
answered Sep 21, 2020 at 21:54
1
  • can you please update the full code.... where you have added the code in asked question code. Commented Mar 28, 2022 at 14:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.