d
Topic
Posts:
2
7 months ago

Buy X get Y Free based on a specific product This post is outdated

Hello there,

I have currently modified a function in my cart.liquid based of the github in this thread https://ecommerce.shopify.com/c/ecommerce-design/t/adding-companion-product-to-checkout-306269

Right now the function currently adds a free pillow when a mattress is added to the cart. If another item is added to the cart it adds another free pillow to the cart. I just need it to be specific just to the mattress. 

Here's the code maybe I'm missing something but I feel as though I've almost have this figured out. I've tried using item_id, product_handle, and nothing seems to be working.

{% comment %}
  To add a companion product to the cart automatically if a primary product is in cart:
  1. Create a new link list under your Navigation tab.
  2. In that link list, make the first link point to companion product.
  3. Copy your link list handle where indicated at line 9
  4. Set the minimum cart total required for the bonus product on line 10
{% endcomment %}

{% assign linklist = linklists['free-product'] %}
{% assign min_total = 30 %}

{% if linklist.links.size > 0 %}

  <script>!window.jQuery && document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"><\/script>')</script>
  
  <script>
  if (typeof Shopify === 'undefined') var Shopify = {};
	Shopify.cart = {{ cart | json }};                    //initialize the cart
	Shopify.toAdd = {{ linklist.links.first.object.variants.first.id }}; //Id of link list gift with purchase item equals - 8612890345514
      
    var product = Shopify.toAdd;
    
	var addPillow = false;                 //if true add pillow
	var removePillow = false;              //if true remove pillow
    var freePillowCount = 0;
    var freePillowQual = 0;
    var quantity = 0;
      
      
	Shopify.idsInCart = [];    //Array of product id's in Cart
    var cartProducts = Shopify.idsInCart;
       
	for (var i=0; i<Shopify.cart.items.length; i++) {     //loop for every item in the cart
	  Shopify.idsInCart.push(Shopify.cart.items[i].id);  //build array of Id's from Cart
        
	  //if total weight more than 30, and the current product id isn't equal to the free pillows id
		if ((Shopify.cart.total_weight >= {{ min_total }}) && (Shopify.cart.items[i].id !== Shopify.toAdd)) {
            
	    	addPillow = true;
            freePillowQual = freePillowQual + Shopify.cart.items[i].quantity; // item_count != link list id 
		}
	   else { // Bonus item is in cart
			removePillow = true;	
            freePillowCount = freePillowCount + Shopify.cart.items[i].quantity; // item_count == Link List id items
		
          
        if ( Shopify.cart.items.length == 1 ){
              Shopify.clear(); }
        }
	}
      
      // alert('Free Pillow Qualified Items = '+freePillowQual); //sets quantity for link list item   
      // alert('Free Pillow Items = '+freePillowCount);

      quantity = freePillowQual;
      
      if (freePillowQual === 0) {
          // alert('no qualified items');
      }
      
      if ( freePillowCount < freePillowQual  ) {
          
        var params = {
      	type: 'POST',
      	url: '/cart/add.js',
      	data: 'quantity=' + quantity + '&id=' + Shopify.toAdd,
      	dataType: 'json',
      	success: function(line_item) { 
        window.location.href = '/cart'; }
        }
    jQuery.ajax(params);
   }
   if ( freePillowCount !== freePillowQual ) {
     var quantity2 = freePillowQual;
     
		var paramsUpdate = {
      	type: 'POST',
      	url: '/cart/change.js',
      	data: 'quantity=' + quantity2 + '&id=' + Shopify.toAdd,
      	dataType: 'json',
      	success: function(line_item) { 
          window.location.href = '/cart'; }
        }
    jQuery.ajax(paramsUpdate);
   }  
      
     
  
  </script>

{% endif %}

{% comment %}
 The contents of the cart.liquid template can be found in /sections/cart-template.liquid
{% endcomment %}

{% section 'cart-template' %}

 

i
Replies
Posts:
2
7 months ago

so I've simplified the code but now it only adds one free gift based on specific product in the cart, is it possible to add and subtract based on the specific product?

Here's the new code

{% assign linklist = linklists['free-product'] %}
{% assign min_total = 30 %}
{% assign item_total = 1 %}


{% if linklist.links.size > 0 %}

  <script>!window.jQuery && document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"><\/script>')</script>
  
  <script>
  if (typeof Shopify === 'undefined') var Shopify = {};
	Shopify.cart = {{ cart | json }};
	Shopify.toAdd = {{ linklist.links.first.object.variants.first.id }};
    
	var pleaseAdd = false;
	var pleaseRemove = false;
    var freePillow = 0;
    
	Shopify.idsInCart = [];
  
	for (var i=0; i<Shopify.cart.items.length; i++) {
	  Shopify.idsInCart.push(Shopify.cart.items[i].id);
	  
      if (Shopify.cart.items[i].id !== Shopify.toAdd) {
        
        if (Shopify.cart.total_weight >= {{ min_total | times: 100 }}) {
	    	pleaseAdd = true;
            freePillow = freePillow + Shopify.cart.items[i].quantity
		}
	  } else { // Bonus item is in cart
            
        
        if (Shopify.cart.total_weight < {{ min_total | times: 100 }} && Shopify.cart.item_count < {{ item_total | times: 100 }}) {
			pleaseRemove = true;
		}
      }
	}
    
    quantity = freePillow;

  if (pleaseAdd && (jQuery.inArray(Shopify.toAdd, Shopify.idsInCart) === -1)) {
    var params = {
      type: 'POST',
      url: '/cart/add.js',
      data: 'quantity=1&id=' + Shopify.toAdd,
      dataType: 'json',
      success: function(line_item) { 
        window.location.href = '/cart';
      }
    };
    jQuery.ajax(params);
    
  } if (pleaseRemove) {
    var params = {
      type: 'POST',
      url: '/cart/update.js',
      data: 'updates[' + Shopify.toAdd + ']=0',
      dataType: 'json',
      success: function(line_item) { 
        window.location.href = '/cart';
      }
    };
    jQuery.ajax(params);
  }
  
  </script>

{% endif %}

{% comment %}
 The contents of the cart.liquid template can be found in /sections/cart-template.liquid
{% endcomment %}

{% section 'cart-template' %}