Created
May 20, 2013 20:16
-
-
Save gterrill/5615152 to your computer and use it in GitHub Desktop.
Add a variant to the cart dynamically depending on what is in the cart.
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
{{ 'api.jquery.js' | shopify_asset_url | script_tag }} | |
<script type="text/javascript"> | |
var variant = 231075270, // delivery item | |
count = 0, existing = 0, cart = {{ cart | json }}; | |
var returnToCart = function(line_item) { | |
window.location.href = '/cart'; | |
}; | |
for (var i = 0; i < cart.items.length; i++) { | |
if (cart.items[i].id != variant) { | |
count += count + cart.items[i].quantity; | |
} else { | |
existing = cart.items[i].quantity; | |
} | |
} | |
// add delivery variant to match number of items in the cart | |
if (count != existing) { | |
if (existing > 0) { | |
Shopify.changeItem(variant, count, returnToCart); | |
} else { | |
Shopify.addItem(variant, count, returnToCart); | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment