Skip to content

Instantly share code, notes, and snippets.

@panoply
Created June 3, 2016 21:52
Show Gist options
  • Save panoply/455ce7943c59cdcd2807c7920d71b3b8 to your computer and use it in GitHub Desktop.
Save panoply/455ce7943c59cdcd2807c7920d71b3b8 to your computer and use it in GitHub Desktop.
Shopify Scripts – Free product with purchase of Item. Also requires a Liquid integration.
//
{% assign found = false %}
{% for finder in product.collections %}
{% if finder.handle == 'name' %}
{% assign found = true %}
{% endif %}
{% endfor %}
{% if found == false %}
<ul>
{% for product in collections.knitwear.products %}
{% if product.type == "type or tags" %}
{% for variant in product.variants %}
<li><input type="checkbox" name="id[]" value="{{ variant.id }}" />
<a href="{{ product.url }}" target="_blank" title="More details">
{{ product.title }} - Valued at {{ product.price | money }}
</a>
</li>
{% endfor %}
{% endif %}
{% endfor %}
</ul>
{% endif %}
cart = Output.cart = Input.cart
jacket_count = cart.line_items.select do |l|
l.variant.product.product_type == "Jacket"
end.reduce(0) { |a, l| a + l.quantity }
beanies = cart.line_items.select { |l| l.variant.product.product_type == "Beanie" }
beanies.each do |l|
break if jacket_count == 0
if jacket_count < l.quantity
split = l.split(take: jacket_count)
split.change_line_price(Money.zero, message: "Free Beanie")
cart.line_items << split
break
else
l.change_line_price(Money.zero, message: "Free Beanie")
jacket_count -= l.quantity
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment