In our store, I wrote a script that if a customer buys 1 out of 13 total valid items, another item (y) would be automatically added to their cart and be made free. The issue I am running into is that every time the customer removes that free item from their cart, the cart-template page reloads and reruns my script, which automatically adds that free item back to the customers cart.
My goal is that although we automate the processing of adding the "free item" to the cart, the customer still has the ability to remove it if they chose, and even increase its quantity, though only one will remain free.
Attached is my JS that executes the above code :
{% assign liveCode = "yes" %}
{% assign bootmodeList =
"3932518121587,6631396442197,3957106442355,2017147617395,1658735951987,1561223331955,1561223430259,4810721853525,1658760495219,1561223397491,4698621739093,1658762166387,4760306810965" | split : ',' %}
{% assign product_to_add_auto = all_products['test-test-test-enet-cable-bootmod3-flashing-and-f-series-and-g-series-coding-cable'] %}
{% assign start = "2022-10-18" | date: '%s' %}
{% assign end = "2022-10-19" | date: '%s' %}
{% assign today = "now" | date: '%s' %}
{% if start <= today and today <= end %}
{% unless cart.item_count == 0 or product_to_add_auto.empty? or product_to_add_auto.variants.first.available == false %}
{% assign variant_id = product_to_add_auto.variants.first.id %}
{% if liveCode == "yes" %}
{% if product_to_add_auto.available == true %}
{% assign isProduct = false %}
{% for item in cart.items %}
{% assign product_id = item.product_id | append:"" %}
{% if bootmodeList contains product_id %}
{% assign isProduct = true %}
{% endif %}
{% endfor %}
{% if isProduct == true %}
{{ product_to_add_auto | json }}
<script>
(function(jquery) {
let cartItems = {{ cart.items | json }},
qtyInTheCart = 0,
cartUpdates = {};
console.log(cartItems);
for (let i = 0; i < cartItems.length; i++) {
if (cartItems[i].id === {{ variant_id }}) {
qtyInTheCart = cartItems[i].quantity;
break;
// this checks the cart to prevent double addition of wifi adapter
}
}
if ((cartItems.length === 1) && (qtyInTheCart > 0)) {
cartUpdates = {
{{ variant_id }}: 0
}
// if wifi adapter is already in cart by itself without bootmode, remove it.
} else if ((cartItems.length >= 1) && (qtyInTheCart !== 1)) {
cartUpdates = {
{{ variant_id }}: 1
}
// adds wifi adapter to cart if bootmode is in cart and theres not one already
} else {
return;
// if none are true, code doesnt run "catch all"
}
// http response object
const params = {
type: 'POST',
url: '/cart/update.js',
data: {
updates: cartUpdates
},
dataType: 'json',
success: function(stuff) {
window.location.href = '/cart'; // reloads to cart on successful post request
}
};
jquery.ajax(params);
// fires ajax request using jquery
})(jQuery);
</script>
{% endif %}
{% endif %}
{% endif %}
{% endunless %}
{% endif %}
My first attempt at solving this involved creating a unique ID on the "remove" button of the shopping cart and adding an event listener to it with a ".preventDefault()" function attached to it, but it prevented the entire code from working and removed the ability to auto add the free item.
2
Answers
Taking from the answer posted above, here is what I was able to come up :
const params = { type: 'POST', url: '/cart/update.js', data: { updates: cartUpdates }, dataType: 'json', success: function(stuff) { window.location.href = '/cart'; // reloads to cart on successful post request } };
It is pretty simple. If you added the free item to the cart in the first place, you used a Javascript call to do that. So set your localStorage to have a key:value pair of something like "free": true.
Anytime you go to run your add this free thing to the cart, check if that key exists. If it does not, add the free thing. If it does exist, it means you already added a freebie to the cart, so don’t do that again. If the customer chose to remove it, so be it, now it won’t be re-added. You can then remove that key if you want on the way to checkout, or whatever… leave it.