Skip to content

Instantly share code, notes, and snippets.

@cpres
Forked from tkirrane/companion-product
Last active July 26, 2021 13:36
Show Gist options
  • Save cpres/0449e279a9f0edbd4d6c to your computer and use it in GitHub Desktop.
Save cpres/0449e279a9f0edbd4d6c to your computer and use it in GitHub Desktop.
{% 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['put-your-link-list-handle-here'] %}
{% assign min_total = 100 %}
{% comment %}
You're done. Include this code in cart.liquid at the top or bottom.
{% endcomment %}
{% 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;
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_price >= {{ min_total | times: 100 }}) {
pleaseAdd = true;
}
} else { // Bonus item is in cart
if (Shopify.cart.total_price < {{ min_total | times: 100 }}) {
pleaseRemove = true;
}
}
}
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);
} else 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 %}
@HarbrechtDesign
Copy link

This is exactly the code I needed to write when I found the companion item thread. Thank you for this.

@bgsn
Copy link

bgsn commented Dec 13, 2017

how do you limit the quantity of 'bonus items' (or companion items) that can be added to cart once a purchase hits the minimum? This code worked but if you click on the item that was added to the cart, it allows you to change the quantity. I want to ensure that is limited. Please let me know. Any help is much appreciated! Thanks!!

@Faeve
Copy link

Faeve commented May 30, 2019

This is awesome. Thank you. Is there a way to choose which product gets the free gift, not just the total $?

Also, do you know of a way to remove the ability for them to adjust the quantity of the gifts received?

Example attached. :) Thanks for your help!

Free gift shopping cart example

@artosousa
Copy link

@Faeve did you ever get what you were looking for figured out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment