forked from carolineschnapp/carolineschnapp-gift-wrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gift-wrapping-multiple-charge.liquid
107 lines (91 loc) · 3.69 KB
/
gift-wrapping-multiple-charge.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!-- DO NOT REMOVE THE FOLLOWING COMMENT -->
<!-- (c) Copyright 2014 Caroline Hill. All Rights Reserved. Contact Mlle Caroline Schnapp at [email protected] -->
<!-- DO NOT REMOVE THE ABOVE COMMENT -->
{% if linklists.gift-wrapping.links.size > 0 and linklists.gift-wrapping.links.first.type == 'product_link' %}
<div id="is-a-gift" style="clear: left; margin: 30px 0" class="clearfix rte">
<p>
<input type="hidden" name="attributes[gift-wrapping]" value="" />
<input id="gift-wrapping" type="checkbox" name="attributes[gift-wrapping]" value="yes" {% if cart.attributes.gift-wrapping %} checked="checked"{% endif %} style="float: none" />
<label for="gift-wrapping" style="display:inline; padding-left: 5px; float: none;">
For {{ linklists.gift-wrapping.links.first.object.price | money }} per item,
please wrap the products in this order.
</label>
</p>
<p>
<label style="display:block" for="gift-note">Gift message (free and optional):</label>
<textarea name="attributes[gift-note]" id="gift-note">{{ cart.attributes.gift-note }}</textarea>
</p>
</div>
{% assign id = linklists.gift-wrapping.links.first.object.variants.first.id %}
{% assign gift_wraps_in_cart = 0 %}
{% for item in cart.items %}
{% if item.id == id %}
{% assign gift_wraps_in_cart = item.quantity %}
{% endif %}
{% endfor %}
{% assign items_in_cart = cart.item_count | minus: gift_wraps_in_cart %}
<style>
#updates_{{ id }} { display: none; }
</style>
<script>
Shopify.Cart = Shopify.Cart || {};
Shopify.Cart.GiftWrap = {};
Shopify.Cart.GiftWrap.set = function() {
jQuery.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { {{ id }}: {{ items_in_cart }} }, attributes: { 'gift-wrapping': true } },
dataType: 'json',
success: function() { location.href = '/cart'; }
});
}
Shopify.Cart.GiftWrap.remove = function() {
jQuery.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { {{ id }}: 0 }, attributes: { 'gift-wrapping': '' } },
dataType: 'json',
success: function() { location.href = '/cart'; }
});
}
// If we have nothing but gift-wrap items in the cart.
{% if cart.items.size == 1 and gift_wraps_in_cart > 0 %}
jQuery(function() {
Shopify.Cart.GiftWrap.remove();
});
// If we don't have the right amount of gift-wrap items in the cart.
{% elsif gift_wraps_in_cart > 0 and gift_wraps_in_cart != items_in_cart %}
jQuery(function() {
Shopify.Cart.GiftWrap.set();
});
// If we have a gift-wrap item in the cart but our gift-wrapping cart attribute has not been set.
{% elsif gift_wraps_in_cart > 0 and cart.attributes.gift-wrapping == blank %}
jQuery(function() {
Shopify.Cart.GiftWrap.set();
});
// If we have no gift-wrap item in the cart but our gift-wrapping cart attribute has been set.
{% elsif gift_wraps_in_cart == 0 and cart.attributes.gift-wrapping != blank %}
jQuery(function() {
Shopify.Cart.GiftWrap.set();
});
{% endif %}
// When the gift-wrapping checkbox is checked or unchecked.
jQuery(function() {
jQuery('[name="attributes\[gift-wrapping\]"]').change(function() {
if (jQuery(this).is(':checked')) {
Shopify.Cart.GiftWrap.set();
}
else {
Shopify.Cart.GiftWrap.remove();
}
});
});
</script>
{% else %}
<p style="clear: left; margin: 30px 0" class="rte">
You attempted to add a gift-wrapping script to your shopping cart, but it won't work because you don't have
a link list with handle <code>gift-wrapping</code> which, in turn, contains a link
to your gift-wrapping product. Please review the steps outlined
<a href="https://shopify.dev/tutorials/customize-theme-add-gift-wrap-option" target="_blank" rel="noopener noreferrer nofollow">here</a>.
</p>
{% endif %}