A developer is experiencing cart duplication issues when using JavaScript to automatically add a free product (Product Y) for a “Buy X, Get Y” promotion triggered at $500+ cart value.
The Problem:
When an automatic discount code is active, adding Product Y via fetch('/cart/add.json') causes the entire cart contents to duplicate as new line items
Without the discount code active, the JavaScript works correctly with no duplication
Manual addition of Product Y with the discount also works properly
Troubleshooting Attempted:
Developer confirmed the script only triggers once when cart reaches $500+
Tested using both id and product_id properties in the product object
Screenshots show normal cart behavior vs. the error state with duplicated items
Using Shopify’s automatic discount configuration method
Current Status:
The issue appears isolated to the interaction between the JavaScript add-to-cart function and Shopify’s automatic discount system. The developer suspects a missing parameter in the line item properties but hasn’t identified the solution yet.
Summarized with AI on November 14.
AI used: claude-sonnet-4-5-20250929.
I’m having a strange behavior in the cart page when I add a product using javascript. The logic is simple, I need to apply a discount (Buy X and get Y) automatically.
For this to work, product Y needs to exist in the cart and a simple condition has to be true. (The shopping cart has to be equal or more than $500)
So, when my customer has $500 or more, my javascript adds the product Y to the cart using this logic:
It works perfectly.
The rare behaviour happens when the discount code is active. The cart creates a new line with all my products duplicating the contents of the cart. What could be happening?
Important info:
I don’t apply the discount code in my logic, i only add the product Y.
I’m adding screenshots of the issue.
I’m using this configuration of discount code with Automatic method.
I believe the behavior you’re experiencing might be due to how Shopify handles automatic discounts. When an automatic discount is applied, Shopify creates a new line item for the discounted product. This is intended and is done to show the original and discounted price separately.
However, in your case, it seems like the entire cart is being duplicated. This could be due to a conflict with your script and how Shopify applies automatic discounts. You should ensure that your script is only adding the product Y to the cart once the cart total is $500 or more and make sure it’s not being triggered multiple times.
Also you could test without the script and manually add products to the cart to reach the $500 threshold. Then, check to see if the cart is still being duplicated. If it’s not, then the issue might be with your script.
I tried applying product Y and the discount manually and it works correctly.
The problem, as it seems, is in my JS .
However, when I have the discount code disabled, my JS adds product Y correctly and my cart stays with the original items (no duplicity). This only happens when I have the discount code active. I am using exactly this code in my JS (https://shopify.dev/docs/api/ajax/reference/cart)