我正在尝试总共做4件事。我已经尝试了多种方法,但是它从未起作用,可以使用一些帮助。
此代码来自免费的礼物代码,我对其进行了编辑以增加核心费用。如果我至少能看到如何为这些命令编写代码,我相信我可以做其余的事情。
{% assign core_charges_in_cart = 0 %}
{% for item in cart.items %}
{% if item.product.tags contains 'core2' %}
{{ assign core_charges_in_cart = core_charges_in_cart | plus: 1 }}
{% endif %}
{% endfor %}
{% assign items_in_cart = cart.item_count | minus: core_charges_in_cart %}
{% assign core_in_cart = 0 %}
{% for item in cart.items %}
{% if item.product.tags contains 'CC2' %}
{{ assign core_in_cart = core_in_cart | plus: 1 }}
{% endif %}
{% endfor %}
<style>
#updates_{{ id }} { display: none; }
</style>
<script>
Shopify.Cart = Shopify.Cart || {};
Shopify.Cart.GiftWrap = {};
Shopify.Cart.GiftWrap.set2 = function() {
jQuery.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { {{ id }}: {{ items_in_cart }} }, attributes: { 'core-charge-2': true } },
dataType: 'json',
success: function() { location.href = '/cart'; }
});
}
Shopify.Cart.GiftWrap.remove2 = function() {
jQuery.ajax({
type: 'POST',
url: '/cart/update.js',
data: { updates: { {{ id }}: 0 }, attributes: { 'core-charge-2': '' } },
dataType: 'json',
success: function() { location.href = '/cart'; }
});
}
// If we have nothing but gift-wrap items in the cart.
{% if cart.items.size == 1 and core_charges_in_cart > 0 %}
jQuery(function() {
Shopify.Cart.GiftWrap.remove2();
});
// If we don't have the right amount of gift-wrap items in the cart.
{% elsif core_in_cart < core_charges_in_cart %}
jQuery(function() {
Shopify.Cart.GiftWrap.set2();
});
{% endif %}
// When the core_charges checkbox is checked or unchecked.
jQuery(function() {
jQuery('[name="attributes\[core-charge-2\]"]').change(function() {
if (jQuery(this).is(':checked')) {
Shopify.Cart.GiftWrap.set2();
}
else {
Shopify.Cart.GiftWrap.remove2();
}
});
});
</script>