Skip to content

Instantly share code, notes, and snippets.

@gterrill
Created May 20, 2013 20:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gterrill/5615152 to your computer and use it in GitHub Desktop.
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.
{{ '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