Skip to content

Commit

Permalink
Fixes issue with adding multiple qty to cart
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin-Magmodules committed Jul 12, 2024
1 parent c9ffdb5 commit 8941277
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions view/frontend/web/js/add-to-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@ define([
'Magento_Customer/js/customer-data',
'domReady!'
], function ($, customerData) {
return function (config, element) {
let actionSelector = '.sqrAddToCart';
_wssq.push(['suggest._bindEvent', 'updateResults', function () {
$(actionSelector).on('submit', function (e) {
e.preventDefault();
var submitButton = $(this).find('button[type="submit"]');
return function() {
document.addEventListener('submit', (e) => {
e.preventDefault();

const form = e.target.closest('form');
const button = form.querySelector('button');
const minicart = document.querySelector('[data-block="minicart"]');

if (form.classList.contains('sqrAddToCart')) {
$.ajax({
type: "GET",
url: $(this).attr('action'),
data: $(this).serialize(), // serialize form data
beforeSend: function () {
submitButton.prop('disabled', true);
$('[data-block="minicart"]').trigger('contentLoading');
type: 'GET',
url: $(form).attr('action'),
data: $(form).serialize(),

beforeSend() {
button.setAttribute('disabled', 'disabled');
minicart.dispatchEvent(new CustomEvent('contentLoading'));
},
success: function (data) {
var sections = ['cart'];
customerData.invalidate(sections);
customerData.reload(sections, true);

success() {
customerData.invalidate(['cart']);
customerData.reload(['cart'], true);
},
error: function (result) {

error() {
$('.sqr-closeButton').trigger('click');
var customerMessages = customerData.get('messages')() || {},
messages = customerMessages.messages || [];
const customerMessages = customerData.get('messages')() || {},
messages = customerMessages.messages || [];

messages.push({
text: 'Something went wrong while adding product to the cart. Please reload page and try again.',
Expand All @@ -35,12 +40,13 @@ define([
customerMessages.messages = messages;
customerData.set('messages', customerMessages);
},
complete: function () {
$('[data-block="minicart"]').trigger('contentUpdated');
submitButton.prop('disabled', false);

complete() {
button.removeAttribute('disabled');
minicart.dispatchEvent(new CustomEvent('contentUpdated'));
}
});
});
}]);
}
});
}
});

0 comments on commit 8941277

Please sign in to comment.