Skip to content

Instantly share code, notes, and snippets.

@carolineschnapp
Last active June 15, 2022 06:42
Show Gist options
  • Save carolineschnapp/ba16d0f12ab9ba7baefe to your computer and use it in GitHub Desktop.
Save carolineschnapp/ba16d0f12ab9ba7baefe to your computer and use it in GitHub Desktop.
Delivery charge collected on the Cart page
<script>
Shopify.Cart = Shopify.Cart || {};
Shopify.Cart.DeliveryCharge = {};
Shopify.Cart.DeliveryCharge.set = function(data) {
jQuery.ajax({
type: 'POST',
url: '/cart/update.js',
data: data,
dataType: 'json',
success: function() { location.href = '/cart'; }
});
};
Shopify.Cart.DeliveryCharge.product = {{ linklists.delivery-charge.links.first.object | to_json }};
$('#select-first-two-digits-of-your-postal-code-delivery-charge').change(function() {
var data = {};
switch( $(this).find('option:selected').index() ) {
case 0:
Shopify.Cart.DeliveryCharge.set( {
updates: {
Shopify.Cart.DeliveryCharge.product.variants[0].id: 1,
Shopify.Cart.DeliveryCharge.product.variants[1].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[2].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[3].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[4].id: 0
}, attributes: {
select-first-two-digits-of-your-postal-code-delivery-charge: '01-$5.00'
}
} );
break;
case 1:
Shopify.Cart.DeliveryCharge.set( {
updates: {
Shopify.Cart.DeliveryCharge.product.variants[0].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[1].id: 1,
Shopify.Cart.DeliveryCharge.product.variants[2].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[3].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[4].id: 0
}, attributes: {
select-first-two-digits-of-your-postal-code-delivery-charge: '02-$8.00'
}
} );
break;
case 2:
Shopify.Cart.DeliveryCharge.set( {
updates: {
Shopify.Cart.DeliveryCharge.product.variants[0].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[1].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[2].id: 1,
Shopify.Cart.DeliveryCharge.product.variants[3].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[4].id: 0
}, attributes: {
select-first-two-digits-of-your-postal-code-delivery-charge: '03-$10.00'
}
} );
break;
case 3:
Shopify.Cart.DeliveryCharge.set( {
updates: {
Shopify.Cart.DeliveryCharge.product.variants[0].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[1].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[2].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[3].id: 1,
Shopify.Cart.DeliveryCharge.product.variants[4].id: 0
}, attributes: {
select-first-two-digits-of-your-postal-code-delivery-charge: '30-39 $5.00'
}
} );
break;
case 4:
Shopify.Cart.DeliveryCharge.set( {
updates: {
Shopify.Cart.DeliveryCharge.product.variants[0].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[1].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[2].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[3].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[4].id: 1
}, attributes: {
select-first-two-digits-of-your-postal-code-delivery-charge: '41-49 $3.00'
}
} );
break;
default:
Shopify.Cart.DeliveryCharge.set( {
updates: {
Shopify.Cart.DeliveryCharge.product.variants[0].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[1].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[2].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[3].id: 0,
Shopify.Cart.DeliveryCharge.product.variants[4].id: 0
}, attributes: {
select-first-two-digits-of-your-postal-code-delivery-charge: ''
}
} );
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment