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 }}
{% endif %}
{% endif %}
{% endif %}
{% endunless %}
{% endif %}