Skip to content

Instantly share code, notes, and snippets.

@lucased
Created October 30, 2014 11:03
Show Gist options
  • Save lucased/b470545f531ac3e181eb to your computer and use it in GitHub Desktop.
Save lucased/b470545f531ac3e181eb 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();
})
@wog222
Copy link

wog222 commented Aug 15, 2017

Anyway to adjust the code above to automatically add a "processing and handling" fee of $19.95 to the cart when they add a product?
(optional)And maybe also have a small image thumb to represent what the fee covers there as well?

Thanks for any help!

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