-
-
Save KamGraphica/72ac5652095b4b57227bc9968cf19933 to your computer and use it in GitHub Desktop.
Shopify - Add product to cart automatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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