Skip to content

Instantly share code, notes, and snippets.

@kellyvaughn
Created December 3, 2016 21:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kellyvaughn/2c3c2543c959133e7a2c67bd9a04eb8b to your computer and use it in GitHub Desktop.
Save kellyvaughn/2c3c2543c959133e7a2c67bd9a04eb8b to your computer and use it in GitHub Desktop.
(Shopify) Snippet to auto add product to cart based on Bold Product Options selection (one-time fee)
{% assign totes_addon = all_products['custom-tote-new-order-fee'] %}
{% assign pouches_addon = all_products['custom-pouches-new-order-fee'] %}
{% assign cards_addon = all_products['custom-cards-new-order-fee'] %}
{% unless cart.item_count == 0 %}
{% assign totes_id = totes_addon.variants.first.id %}
{% assign pouches_id = pouches_addon.variants.first.id %}
{% assign cards_id = cards_addon.variants.first.id %}
<script>
(function($) {
var cartItems = {{ cart.items | json }},
qtyInTheCart = 0,
new_totes = 0,
new_pouches = 0,
new_cards = 0,
customOrder = false,
sumCustom = new_totes + new_pouches + new_cards,
cartUpdates = {};
for (var i=0; i<cartItems.length; i++) {
if ( cartItems[i].id === {{ totes_id }} || cartItems[i].id === {{ pouches_id }} || cartItems[i].id === {{ cards_id }} ) {
qtyInTheCart = qtyInTheCart + cartItems[i].quantity;
} else {
if(cartItems[i]['properties']){
if(~cartItems[i]['properties']['Is this a new custom order?'].indexOf("Yes")){
var customOrder = true;
if(~cartItems[i].product_title.indexOf("Pouch")){
new_pouches = new_pouches + 1;
}
if(~cartItems[i].product_title.indexOf("Tote")){
new_totes = new_totes + 1;
}
if(~cartItems[i].product_title.indexOf("Card")){
new_cards = new_cards + 1;
}
sumCustom = new_totes + new_pouches + new_cards;
}
}
}
}
if ( sumCustom !== qtyInTheCart ) {
cartUpdates = { {{ totes_id }}: new_totes, {{ pouches_id }}: new_pouches, {{ cards_id }}: new_cards }
}
else {
return;
}
var params = {
type: 'POST',
url: '/cart/update.js',
data: { updates: cartUpdates },
dataType: 'json',
success: function(stuff) {
window.location.href = '/cart';
}
};
$.ajax(params);
})(jQuery);
</script>
{% endunless %}
@wog222
Copy link

wog222 commented Aug 15, 2017

Anyway to adjust the code above to add a "processing and handling" fee of $19.95 to the cart when they add a product?
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