I’m looking for a solution to automatically add a free gift when a customer spends a certain amount.
It needs to be without an app. I have written the js etc. to add it to a cart page automatically, but have not been able to get this to work with the cart drawer.
It would also be fine to auto add it to checkout but I’m not sure if this is possible with checkout extensibility. I only see a way to do an upsell not a free gift.
Here is what I have that works for the cart page.
{% assign free_gift_threshold_2 = settings.free-gift-threshold-2 | times:100 %}
{% assign variant_id = '42765098975425' %}
<script>
(function($) {
var cartItems = {{ cart.items | json }},
qtyInTheCart = 0,
cartUpdates = {},
cartTotal = {{ cart.total_price }};
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 ) && (cartTotal >= {{ free_gift_threshold_2 }} ) ) {
cartUpdates = { {{ variant_id }}: 1 }
}
else if ( ( cartItems.length >= 1 ) && ( qtyInTheCart > 0 ) && (cartTotal < {{ free_gift_threshold_2 }} ) ) {
cartUpdates = { {{ variant_id }}: 0 }
}
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>