Skip to content

Instantly share code, notes, and snippets.

@seedcms
Created January 22, 2017 00:39
Show Gist options
  • Save seedcms/2e5f05c32aacbefc27bc846e18ddc501 to your computer and use it in GitHub Desktop.
Save seedcms/2e5f05c32aacbefc27bc846e18ddc501 to your computer and use it in GitHub Desktop.
Add to Cart JS
(function(){
$('.add_to_cart').on('click', function(e){
var variantId = VARIANTID GOES HERE;
jQuery.getJSON('/cart.js', function(data){
var items = data.items;
console.log(items);
var currentVarId;
var quantity;
var isProduct = false;
items.forEach(function(e,i,a){
console.log(e);
currentVarId = e.variant_id;
if(currentVarId === variantId) {
isProduct = true;
quantity = e.quantity;
}
});
if(!isProduct) {
jQuery.post('/cart/add.js', {
quantity: 1,
id: variantId
});
} else {
if(quantity > 1) {
jQuery.post('/cart/change.js', {
quantity: 1,
id: variantId
});
}
}
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment