Can't remove free gift in cart

Highlighted
New Member
7 0 0

Hi all, 

 

I set up a free gift membership when customers add a certain product to their cart with the below code following code. Basically, it loops through the cart for a specific product id and once found it adds the free gift and updates the cart. Only problem is if you try and remove the free gift the cart reloads - runs through the below code again and readds the free gift. How can I prevent this behavior? Once a customer clicks remove can the css update, but not do a full reload of the cart? I'm using ajax cart btw.

 

Thanks for the help!!

 

//-----------Add membership product to cart when free wine glass -------
    let cart = $.getJSON('/cart.js');
    cart.done(function(){
      cart = cart.responseJSON;
      let items = cart.items;
      let item_in_cart_already = false;
      
      

//-----------Add memebership to free wine glass + free shipping ------   
// product to look for - free wine glass 
      let item_in_cart_prod_id = 4564217495649;
// product to add - monthly membership recurring
      let item_to_add_prod_id = 4552096383073;
  // monthly membership variant id
      let item_to_add_variant_id = 31971529916513;
      
      
      
//-----------Add free memebership to free wine glass + charge shipping ------   

// product to look for - wine glass free + shipping
      let wine_glass_in_cart = 4564310392929;
 //poduct to add - free monthly membership
      let free_membership = 4564316094561
 // free monthly membership variant id
      let free_membership_variant_id = 32020513488993;


      for(var i = 0; i < items.length; i++){
        if (items[i].product_id == item_to_add_prod_id){
          item_in_cart_already = true;
        }
        if (items[i].product_id == free_membership){
          item_in_cart_already = true;
        }
      }

      if(!item_in_cart_already){
        for(var i = 0; i < items.length; i++){
          if (items[i].product_id == item_in_cart_prod_id){
            let variant_quantity = items[i].quantity;
            Shopify.addItem(item_to_add_variant_id, variant_quantity);
            setTimeout(function(){
              Shopify.getCart(buildCart)
            },1000)
      
          }
           if (items[i].product_id == wine_glass_in_cart){
            let variant_quantity = items[i].quantity;
            Shopify.addItem(free_membership_variant_id, variant_quantity);
            setTimeout(function(){
              Shopify.getCart(buildCart)
            },1000)
            
          }
        }
      }
    })    
0 Likes