Created
December 14, 2018 16:19
-
-
Save vishalarora91/ca6d97dde12c965e0381401ad89812b9 to your computer and use it in GitHub Desktop.
Automatically add Free Gift in Cart
This file contains 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
{% comment %} | |
Replace the text below with the handle of your add-on product. | |
{% endcomment %} | |
{% assign product = all_products['PRODUCT-HANDLE'] %} | |
<!-- Replace Product Handle with the handle of your product --> | |
{% unless cart.item_count == 0 or product.empty %} | |
{% if product.variants.size > 1 %} | |
{% for variant in product.variants %} | |
{% if variant.available != false %} | |
{% assign variant_id = variant.id %} | |
{% break %} | |
{% endif %} | |
{% endfor %} | |
{% else %} | |
{% assign variant_id = product.variants.first.id %} | |
{% endif %} | |
<script> | |
(function($) { | |
/* Replace Product id with the id of your product */ | |
$('.remove-1438100750406').click(function(){ | |
setCookie('no-free-gift', false, 1); | |
}); | |
if(getCookie('secondcase') == 'true'){ | |
var cartItems = {{ cart.items | json }}, | |
qtyInTheCart = 0, | |
cartUpdates = {}; | |
for (var i=0; i<cartItems.length; i++) { | |
if ( cartItems[i].id === {{ variant_id }} ) { | |
qtyInTheCart = cartItems[i].quantity; | |
break; | |
} | |
} | |
if ( ( cartItems.length === 1 ) && ( qtyInTheCart > 0 ) ) { | |
cartUpdates = { {{ variant_id }}: 0 } | |
} | |
else if ( ( cartItems.length >= 1 ) && ( qtyInTheCart !== 1 ) ) { | |
cartUpdates = { {{ variant_id }}: 1 } | |
} | |
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 %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment