Skip to content

Instantly share code, notes, and snippets.

@heri
Last active January 10, 2023 03:00
Show Gist options
  • Save heri/d368d81f7a909474bf8db6bdf41cffbf to your computer and use it in GitHub Desktop.
Save heri/d368d81f7a909474bf8db6bdf41cffbf to your computer and use it in GitHub Desktop.
jQuery.getJSON('/cart.js', function(cart) {
items = cart.items;
var results = items.filter(x => x.id === 6223106801703);
if ((results.length == 0) && (items.length > 0)) {
jQuery.post('/cart/add.js', {
quantity: 1,
id: 6223106801703
});
location.reload();
}
if (results.length > 0 ) {
if ((results.length == 1) && (items.length == 1)) {
// remove free gift if there are no other products in cart
jQuery.post('/cart/update.js', {updates: {6223106801703: 0}});
} else {
// update free gift gift to quantity = 1, for customers adding multiple free gifts
jQuery.post('/cart/update.js', {updates: {6223106801703: 1}});
}
}
});
@heri
Copy link
Author

heri commented Dec 14, 2017

Replace 6223106801703 with the id of the free product

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment