Skip to content

Instantly share code, notes, and snippets.

@KamGraphica
Forked from lucased/shopify-add-to-cart
Created May 11, 2016 21:48
Show Gist options
  • Save KamGraphica/72ac5652095b4b57227bc9968cf19933 to your computer and use it in GitHub Desktop.
Save KamGraphica/72ac5652095b4b57227bc9968cf19933 to your computer and use it in GitHub Desktop.
Shopify - Add product to cart automatically
if (typeof Shopify === 'undefined') var Shopify = {};
Shopify.cart = {{ cart | json }};
Shopify.toAdd = 378589397;
var surchargeInCart = false;
var total = 2507; // total in cents.
for (var i=0; i<Shopify.cart.items.length; i++) {
if (Shopify.cart.items[i].id === Shopify.toAdd) {
surchargeInCart = true;
total -= Shopify.cart.items[i].line_price;
}
}
if (!surchargeInCart && total < 5000 && total >= 0) {
var params = {
type: 'POST',
url: '/cart/add.js',
data: 'quantity=1&id=' + Shopify.toAdd,
dataType: 'json',
success: function(line_item) {
window.location.href = '/cart';
}
};
jQuery.ajax(params);
}
if (surchargeInCart && total >= 5000 || surchargeInCart && total === 0) {
window.location.href = '/cart/change?id=378589397&quantity=0';
}
// Hiding the gift-wrap quantity box and remove button.
jQuery(function() {
jQuery('#updates_' + Shopify.toAdd).hide().parents('li,tr').find('a:last').hide();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment