Skip to content

Instantly share code, notes, and snippets.

@Cam
Last active March 10, 2021 12:35
Show Gist options
  • Save Cam/c2e88ae136ec720238d42fe4a0183860 to your computer and use it in GitHub Desktop.
Save Cam/c2e88ae136ec720238d42fe4a0183860 to your computer and use it in GitHub Desktop.
Shopify automatic discounts in cart without Plus sample code
<script>if (typeof Shopify === "undefined") var Shopify = {}; Shopify.cart = {{ cart | json }};</script>
{% assign all_items_count = 0 %}
{% assign discount_line_total = 0 %}
{% if cart.item_count > 0 %}
{% for item in cart.items %}
{{ item.title }}
{{ item.price | money }}
{% if settings.discounts_enable %}
{% if item.variant.title contains '1L'%}
{% assign items_count = items_count | plus: item.quantity %}
{% assign discount_line_total = discount_line_total | plus: item.line_price %}
{% endif %}
{% endif %}
{% endfor %}
{% if settings.discounts_enable %}
{% if items_count >= 3 %}
{% assign discount = '1LBULK15' %}
{% assign discount_percent = 0.15 %}
{% endif %}
{% if items_count >= 6 %}
{% assign discount = '1LBULK20' %}
{% assign discount_percent = 0.20 %}
{% endif %}
{% if items_count >= 12 %}
{% assign discount = '1LBULK30' %}
{% assign discount_percent = 0.30 %}
{% endif %}
<script>
var discount = '{{ discount }}'
document.getElementById('cart_form').action = "/cart?discount=" + discount;
</script>
{% assign discount_amount = discount_line_total | times: discount_percent | floor %}
{% assign discount_amount_money = discount_amount | money %}
{% assign discounted_total = cart.total_price | minus: discount_amount | floor %}
{% endif %}
{% if settings.flash_discount_enable %}
<script>
var discount = '{{ settings.flash_discount }}'
document.getElementById('cart_form').action = "/cart?discount=" + discount;
</script>
{% endif %}
{% if settings.discounts_enable and discount_percent > 0 %}
{ 'cart.general.subtotal' | t }}
{{ discounted_total | money }}
{{ 'cart.general.savings_html' | t: price: discount_amount_money }}
{% else %}
{{ cart.total_price | money }}
{% endif %}
{% if cart.total_discounts > 0 %}
{% assign savings = cart.total_discounts | money %}
{{ 'cart.general.savings_html' | t: price: savings }}
</p>
{% endif %}
{% else %}
{% comment %}
The cart is empty
{% endcomment %}
{% endif %}
@xxvaiomasterxx
Copy link

Hey Cam what is this code used for? to have multiple automatic discount running?

@Cam
Copy link
Author

Cam commented Feb 26, 2021

@xxvaiomasterxx pretty much, yep! It just uses URL parameters to pre-populated the discount field.

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