Ecommerce Shopify WordPress Discussion

Shopify Liquid and Ajax: Adding and Removing Products in Cart Based on Conditions

I'm working on a Shopify store and trying to implement a feature using Liquid and Ajax. I have a script that adds a free product to the cart when a qualifying product is present. Additionally, I want to remove the free product if the qualifying product is removed from the cart. Here's the existing code I'm using: <script> window.onload = function(){ let cartContainsFreeProduct = false; let cartContainsQualifyingProduct = false; const qualifyingProductVariantId = ***ENTER VARIANT ID OF PRODUCT THAT QUALIFIES FOR FREE PRODUCT***; const freeProductVariantId = ***ENTER VARIANT ID OF THE FREE PRODUCT***; {% for item in cart.items %} if({{ item.id }} === freeProductVariantId){ cartContainsFreeProduct = true; } if({{ item.id }} === qualifyingProductVariantId){ cartContainsQualifyingProduct = true; } {% endfor %} // If cart contains qualifying product and doesn't already contain free product, add qty 1 of free product if(cartContainsQualifyingProduct && cartContainsFreeProduct === false) { jQuery.post('/cart/add.json', { quantity: 1, id: freeProductVariantId }) .done(function() {window.location.reload()}) } } </script> The script for adding the free product seems to be working fine but how do remove the product freeProductVariantId when removing the qualifyingProductVariantId from the cart
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.