-
Notifications
You must be signed in to change notification settings - Fork 24
/
cart-add-on.liquid
50 lines (39 loc) · 1.14 KB
/
cart-add-on.liquid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{% comment %}
Replace the text below with the handle of your add-on product.
{% endcomment %}
{% assign product = all_products['put-your-product-handle-here'] %}
{% unless cart.item_count == 0 or product.empty? or product.variants.first.available == false %}
{% assign variant_id = product.variants.first.id %}
<script>
(function($) {
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 %}