Created
September 1, 2015 16:49
-
-
Save rameshanandakrishnan/cea2ae42be7e01ed88f0 to your computer and use it in GitHub Desktop.
Shopify support multiple gift wrapping options - modified Caroline Schnapp's gift wrapper code
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
<!-- Add a flat charge per order --> | |
<!-- DO NOT REMOVE THE FOLLOWING COMMENT --> | |
<!-- (c) Copyright 2014 Caroline Hill. All Rights Reserved. Contact Mlle Caroline Schnapp at mllegeorgesand@gmail.com --> | |
<!-- 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" class="clearfix rte"> | |
{% for variant in linklists.gift-wrapping.links.first.object.variants %} | |
<p> | |
<input type="hidden" name="attributes[gift-wrapping]" data-variantId="{{ variant.id }}" value="" /> | |
<input id="gift-wrapping" type="checkbox" name="attributes[gift-wrapping]" data-variantId="{{ variant.id }}" value="yes" {% for item in cart.items %}{% if item.id == variant.id %} checked="checked"{% endif %}{% endfor %} style="float: none" /> | |
{{ variant.image.src }} | |
<label for="gift-wrapping" style="display:inline; padding-left: 5px; float: none;"> | |
For {{ variant.price | money }} please wrap the products in this order. | |
</label> | |
</p> | |
{% endfor %} | |
<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 %} | |
<style> | |
#updates_{{ id }} { display: none; } | |
</style> | |
<script> | |
var variantList = [], | |
giftObj = {}; | |
{% for variant in linklists.gift-wrapping.links.first.object.variants %} | |
variantList.push({{variant.id}}); | |
{% endfor %} | |
Shopify.Cart = Shopify.Cart || {}; | |
Shopify.Cart.GiftWrap = {}; | |
Shopify.Cart.GiftWrap.set = function(obj, selectedItem) { | |
jQuery.ajax({ | |
type: 'POST', | |
url: '/cart/update.js', | |
data: { updates: obj }, | |
dataType: 'json', | |
success: function() { location.href = '/cart'; } | |
}); | |
} | |
Shopify.Cart.GiftWrap.remove = function(obj) { | |
jQuery.ajax({ | |
type: 'POST', | |
url: '/cart/update.js', | |
data: { updates: obj }, | |
dataType: 'json', | |
success: function() { location.href = '/cart'; } | |
}); | |
} | |
{% if cart.items.size == 1 and gift_wraps_in_cart > 0 %} | |
jQuery(function() { | |
Shopify.Cart.GiftWrap.remove(); | |
}); | |
{% endif %} | |
// When the gift-wrapping checkbox is checked or unchecked. | |
jQuery(function() { | |
jQuery('[name="attributes\[gift-wrapping\]"]').change(function() { | |
var selectedItem = jQuery(this).attr("data-variantId"); | |
function createObjectQuntity(element, index, array) { | |
if( element == selectedItem) { | |
giftObj[element] = 1; | |
} else { | |
giftObj[element] = 0; | |
} | |
} | |
if (jQuery(this).is(':checked')) { | |
variantList.forEach(createObjectQuntity); | |
Shopify.Cart.GiftWrap.set(giftObj, selectedItem); | |
} | |
else { | |
selectedItem = 0; | |
variantList.forEach(createObjectQuntity); | |
Shopify.Cart.GiftWrap.remove(giftObj); | |
} | |
}); | |
}); | |
</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="http://docs.shopify.com/manual/configuration/store-customization/page-specific/cart-page/add-a-gift-wrap-option">here</a>. | |
</p> | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://help.shopify.com/en/manual/online-store/themes/themes-by-shopify/vintage-themes/customizing-vintage-themes/add-gift-wrap-option