Personalized checkout and custom promotions with Shopify Scripts
Hi
I have written code when user reduces the cart item quantity to 0 then it is removed from cart. In chrome this is working fine . But in Firefox , when user is reducing the item to 0 then its removing the current item but making the next item value 0 in front end .
Below is the code.
$(document).ready(function() {
var plusButtons = document.querySelectorAll('.plus');
var minusButtons = document.querySelectorAll('.minus');
plusButtons.forEach(function(button) {
button.addEventListener('click', function(event) {
event.preventDefault();
var quantityDiv = button.closest('.quantity');
var input = quantityDiv.querySelector('.quantity-input');
var currentQuantity = parseInt(input.value);
var inventoryQuantity = parseInt(quantityDiv.getAttribute('data-inventory-quantity'));
if (currentQuantity + 1 > inventoryQuantity) {
alert('You cannot add more than the available stock.');
} else {
updateCartQuantity(button.getAttribute('data-line'), currentQuantity + 1, input);
}
});
});
minusButtons.forEach(function(button) {
button.addEventListener('click', function(event) {
event.preventDefault();
event.stopPropagation();
var quantityDiv = button.closest('.quantity');
var input = quantityDiv.querySelector('.quantity-input');
var currentQuantity = parseInt(input.value);
if (currentQuantity > 0) {
updateCartQuantity(button.getAttribute('data-line'), currentQuantity - 1, input);
}
});
});
function updateCartQuantity(line, quantity, input) {
fetch(`/cart/change.js`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
line: line,
quantity: quantity
})
})
.then(response => response.json())
.then(data => {
console.log('Cart updated', data);
input.value = quantity;
// Refresh the page after updating
window.location.reload();
})
.catch(error => {
console.error('Error updating cart:', error);
});
}
});
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024