Add Companion / Multiple products to basket when specific product added to basket

New Member
1 0 0

Hi All,

I was searching high and low for a solution to this, and thought I would share the solution i found.

My problem being that I have certain product that is basically a bundle of multiple products. I knew that bundle apps exist. But they only control the stock levels within Shopify. I needed each bundled product to appear as a product line so that each individual SKU would be passed down the line to our warehouse for packing and fulfilling the order correctly. 

I managed to find a thread from Caroline Schnapp - which was absolutely fantastic and I can not thank her enough;

https://github.com/carolineschnapp/add-product-to-cart-automatically

 

Carolines code adds a specific ''extra'' product to the basket once a product any other product added to the basket.

I changed the code a little so that the specific ''extra'' product is only added to the basket if a specific product is also added to the basket.

{% comment %}

First Make a new Snippet and add the following code.

In the shopify product page for the product that should be present in the cart for the ''extra'' product to be added. add a tag.

Replace the 'product-tag' text below with the tag of your main product.

Replace the 'product-handle' text below with the handle of your extra product.

{% endcomment %}



{% for item in cart.items %} 

{% if item.product.type == "Product-Tag" %}





{% assign product = all_products['Product-handle'] %}





  {% assign variant_id = product.variants.first.id %}



  <script>

  (function($) {



    var cartItems = {{ cart.items | json }},

        qtyInTheCart = 0,

        cartUpdates = {};



    for (var i=0; i<cartItems.length; i++) {

      if ( cartItems[i].id === {{ variant_id }} ) {

        qtyInTheCart = cartItems[i].quantity;

        break;

      }

    }



    if ( ( cartItems.length === 1 ) && ( qtyInTheCart > 0 ) ) {

      cartUpdates = { {{ variant_id }}: 0 }

    }

    else if ( ( cartItems.length >= 1 ) && ( qtyInTheCart !== 1 ) ) {

      cartUpdates = { {{ variant_id }}: 1 }

    }

    else {

      return;

    }



    var params = {

      type: 'POST',

      url: '/cart/update.js',

      data: { updates: cartUpdates },

      dataType: 'json',

      success: function(stuff) { 

        window.location.href = '/cart';

      }

    };



    $.ajax(params);



  })(jQuery);

  </script>





{% endif %}

{% endfor %}



___

 

 

Add this to bottom of cart.liquid

 

{% include 'Snippet-Name' %}

 

0 Likes
Tourist
3 0 2

I signed in to my account so I could reply and say THANK YOU! I have been searching all day for a solution to this!

0 Likes
New Member
1 0 0

Will this code enable multiple specific products and multiple extra products. I mean to say if 4 no of specific products added to cart them 4 numbers of extra products also will be added automatically?

Highly appreciate your response.

0 Likes
New Member
1 0 0

I am also looking for this solution.

0 Likes