For transparency: this is an exact copy of a request that I have made in a Shopify forum, but I did not get any help there (except for an expensive offer to solve the issue…).
I own a Shopify store that uses a popup cart before checkout. I am using the following code on the popup cart, in order to enable customers to increase or decrease the quantity of items in the cart:
<div class="input-group m-0">
<input type="button" value="-" class="button-minus" data-field="quantity">
<input id="{{ item.variant.id }}" type="number" step="1" max="" value="{{ item.quantity }}" name="quantity" class="quantity-field">
<input type="button" value="+" class="button-plus" data-field="quantity" onclick="Shopify.addItem({{ item.variant.id }}, 1)">
</div>
As you might see I am using the Shopify.addItem() function when onclick is triggered in order to increase the amount of items per product in the cart. This function really works. Sadly I did not find any function to reduce the quantity by 1. So therefore I wrote some JavaScript, but it sadly does not work. Find the code here:
$('.input-group').on('click', '.button-minus', function(e) {
e.preventDefault();
var fieldName = $(e.target).data('field');
var parent = $(e.target).closest('div');
var currentVal = parseInt(parent.find('input[name=' + fieldName + ']').val(), 10);
var currentVariant = parseInt(parent.find('input[name=' + fieldName + ']').attr('id'), 10);
jQuery.post('/cart/change.js', { quantity: currentVal - 1, id: currentVariant });
});
2
Answers
I used the results from HymnZ. Sadly the provided code needed small changes to work in my shop. Find it below! And honors to HymnZ for the hint to the solution!
Your
currentVal
¤tVariant
declaration is wrong. Need to add " inside a property+ jQuery.post needs to be in an
aysnc ... await
function. Try thisThe above should work, but wouldn’t it be nifty if we can use
Shopify.removeItem
? Create a function like this and add it to scripts.js.liquid fileand change the quantity buttons to