Add an item to cart automatically if another item is in it

The app, Infinite Options works for this. I got locked into their free version from when they first came out, but not it costs $$ per month. But it worked great, takes a little understanding to get it to work the first time, but its just mouse clicks to get the feature to work.

Thank you for your answer

Hey there! I tried this code but my shop doesn’t seem to have any of the Shopify functions (Shopify.addItem, Shopify.onError, etc.) Where are these functions normally found? I also get this error when it gets to the url: "/cart/add.js" part.

Thank you for this code by the way! Very well written.

Its difficult to keep these codes up to date. As they add more features to the themes they are moving away from stagnant carts and are moving to these dynamic carts, which is great but completely different understanding of code. Most new these have some form of ajax cart which is like an auto refreshing cart and this code won’t work for that unfortunately. I have since moved away from that,

But a similar coding would be: (i did not write all the {} () )

For items in cart.items

If item is …

Add item …

Endif endfor

You mentioned in a previous post that you figured out how to have the extra item added in the backend. How did you do this?

I’d love to find a solution to auto-add item A if item B is purchased, but it’s not a requirement that the customer see this in their cart. What is most important is that when we are fulfilling the order, we’re able to print a shipping label for both items. Manually adjusting each order to account for two boxes is time consuming.

Let me know!

Sean,

I was looking for a way to automatically add a Mason Jar Deposit to Green Juices I sell at my retail store and believe I found a solution, although I’ve not yet fully tested it..

First, I created a product called ā€œMason Jar Depositā€ and set the price to $1.25, unchecked inventory tracking and physical product, and made it active.

Then, I went to Discounts and created an automatic discount using the option ā€˜Buy x, Get y’. Under ā€˜Customer Buys’ I set it to a minimum of (1) item for ā€˜specific product’ ā€œGreen Juiceā€; and under ā€˜Customer Gets’ chose ā€˜specific product’ ā€œMason Jar Depositā€ at a 20% discount (since I actually want the deposit amount to be $1).

Not sure if this will work out the way I want, but thought it might be of help to your situation as well.

Cheers!

~ Bryce

2 Likes

Hi Bryce

Did that solution work for you? I am trying to do something similar for jar deposits at my package-free shop.

Does that solution work if a customer buys more than one products? For example, if a customer buys two green juices, does that solution give them two mason jar deposits?

Thanks for your help!

Heenal

@BG417

Thank you for this. I did mine with coding and then eventually we removed that option because customers couldn’t handle the products correctly.

I believe it should. There are easy ways of testing this. Just create your own test-customer and after you make the edits. Add those items to your cart and see what happens. Good luck!

Hi @Ninthony ,

Thanks for the solution and it’s working fine in my store. That’s great.

Just a small change I don’t know how to do which is that I need one item A to be added to the cart when I add 3 or more of item B. Can you help me with that?

Much appreciated.

Kam

That is why I moved away from the add A if B.

But if you are looking for code it starts with a for loop.

For item in cart.item

If item == A

Add B endif endfor

Thats basically what your code will look like. It should be added on the cart page. The specific code parameters will depend on your theme. Have fun and good luck.

Thanks @seangerke

Actually, all I need is how to set the number of items needed in order to add item B.

The code is working fine for add one you get one item, which is good but I need to change it to buy 3 or more AND get one item.

Thanks for your reply.

Cheers,

Kam,

Do a for loop through the cart items. Then add an if loop that looks for item B and its quantity == 3. Dont forget to stop the loop or it will run forever. Once your parameters are met not sure if its a {% break %} or stop to end the for loop at that time without keeping the search going like endfor

Hope this helps point you in rhe correct path! GL

Show More

Okay let me take a look tomorrow. Im replying on my phone and the code is too difficult to display properly on my system.

Show More

I think you will need to use the function ā€œassignā€ to set the qty to 3. {% assign B.quantity = 3 %} i dont know the qty parameter off the top of my head.

Hi @seangerke

I used this code and it works fine, just I need to edit the part about the quantity:


I’ve tried this and I get an error on inspect:

The code uses jQuery, which it appears your theme may not be using. You can include it in the head of the HTML before the opening body tag:


Thanks @Ninthony I definitely should have thought of that!

But I’m still getting an error, a new one:

Shopify.addItem is not a function. (In 'Shopify.addItem(item_to_add_variant_id, variant_quantity)', 'Shopify.addItem' is undefined)

Hi @Ninthony

It’s working fine, just I need to change it a bit to have this logic:

buy 3 or more of item A AND get one item B.

Much appreciated,

Kamal

You can add this right under where you included jQuery, this is a bunch of little helper functions Shopify provides as an asset:

{{ 'api.jquery.js' | shopify_asset_url | script_tag }}

You can see them in action here:

https://mayert-douglas4935.myshopify.com/pages/api

Shopify.addItem is included in those functions.