Shopify: Automatically add item to cart when another item is added

My client is using Shopify plus, So I don’t know if this will only work on Shopify plus or just regular. But any who this is what we did. We combined the Shopify Automatic Discounts and some custom code to make it happen. We wanted to add a free CD with every purchase of the book. You need to have both products for sale in your store for this to work. And you will only get the CD Free if you order a Book. In the below code if the title of any item in your cart includes book it will automatically add the item that you please to the users Cart.

In cart.liquid

{% if cart.item_count > 0 %}
 {% for item in cart.items %}
 {% if item.product.title contains'Book' %}
 {% comment %}
   Replace the text below with the handle of your add-on product.
 {% endcomment %}
 {% assign product = all_products['the-slug-of-the-product'] %}
 {% unless cart.item_count == 0 or product.empty? or product.variants.first.available == false %}
 {% assign variant_id = product.variants.first.id %}
   (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);   
 {% endunless %}
 {% endif %}
 {% endfor %}
 {% endif %}

This above code ads the item to the users cart if they have anything in there cart that has anything that is titled book.

Then in Shopify Discounts we select if “Buy X get Y”:

Then we can select if they buy anything in the Book Category:

Then we select what the discount that the customer gets, In this case they get the CD Free:

We were using an app and it wasn’t working quite properly, so this made it all good in the hood.

The coroner

I didn’t know what to expect when I went to go pick up your belongings. I had never done anything like that before. I hadn’t read about it in a story or seen anything like it in a movie. I was worried about getting there on time and worried about parking.

I was the only car parked in the parking lot, and there was nobody in line. I went to the window, showed my ID, and they said to wait.

I was called into a room and there I waited. It was like an office but there was only one door and a window with a hole cut out where things get passed through. I brought a bag because I didn’t know what to expect, I didn’t know what my father had on him.

He had some cash on him, but they give you a check and not cash. They gave me his wallet, which was empty and gave me all of what was in it, separately. I don’t remember the contents of what I came home with, but it wasn’t anything that I kept.

Most of it smelled and all of it didn’t have any sentimental value to me.

I was scared about doing it. But I was glad when it was all over.

The building was beautiful, very historic. It somehow made the whole process easier for me.

Margo

Here are the things that I remembered about you:

  • One time you ate the couch in half
  • You would get excited about the windshield wiper, you maybe thought it was a bird
  • When I would drive you in fey, the leaves would come out from underneath the hood and you would look backwards at the leaves flying out of the car
  • The first time I drove you downtown to Erin’s loft, you sat on my lap most of the way
  • It made me laugh when we went to the dog park and when you got tired with catching the ball, you would just go and lay down in the shade