Skip to content

Instantly share code, notes, and snippets.

@DanWebb
Last active October 27, 2021 16:11
Show Gist options
  • Save DanWebb/b922109a24f64ab57752 to your computer and use it in GitHub Desktop.
Save DanWebb/b922109a24f64ab57752 to your computer and use it in GitHub Desktop.
Add a percentage sale to an item listing (ex: 20% off) based off it's compare at price and actual price
{% comment %}
Get a products sale percentage based off it's compare_at_price vs it's price
Example usage:
{% include 'sale_percentage' with product %} Off
Outputs for a product with a compare at price of £10 and a price of £5:
"50% Off"
{% endcomment %}
{% assign was_price = sale_percentage.compare_at_price %}
{% assign now_price = sale_percentage.price %}
{% if was_price > now_price %}
{{ was_price | minus: now_price | times: 100.0 | divided_by: was_price | round }}
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment