first commit
This commit is contained in:
189
snippets/get-display-discount.liquid
Normal file
189
snippets/get-display-discount.liquid
Normal file
@@ -0,0 +1,189 @@
|
||||
{%- comment -%}
|
||||
Pass in:
|
||||
|
||||
- product or variant
|
||||
- format string, one of
|
||||
- "sale_text"
|
||||
- "percent_off"
|
||||
- "minus_percent"
|
||||
- "save_percent"
|
||||
- "amount_off"
|
||||
- "minus_amount"
|
||||
- "save_amount"
|
||||
{%- endcomment -%}
|
||||
|
||||
{%- assign varies = false -%}
|
||||
|
||||
{%- liquid
|
||||
assign prod_price = prod.price
|
||||
assign prod_compare_at_price = prod.compare_at_price
|
||||
|
||||
if variant
|
||||
assign prod = variant
|
||||
|
||||
assign prod_price = variant.metafields.app--168074346497.auto_discounted_price.value | default: variant.price
|
||||
assign prod_compare_at_price = variant.compare_at_price
|
||||
|
||||
if variant.metafields.app--168074346497.discount_type.value != nil and variant.metafields.app--168074346497.discount_type.value != "fixed" and prod.metafields.app--168074346497.discount_percentage.value > 0.01
|
||||
assign deducted_percentage = 1.0 | minus: prod.metafields.app--168074346497.discount_percentage.value
|
||||
|
||||
if variant.metafields.app--168074346497.discount_percentage.value > 0.01
|
||||
assign deducted_percentage = 1.0 | minus: variant.metafields.app--168074346497.discount_percentage.value
|
||||
endif
|
||||
|
||||
assign prod_price = variant.price | divided_by: 100.0 | times: deducted_percentage | times: 100.0 | ceil
|
||||
|
||||
assign prod_compare_at_price = variant.price
|
||||
|
||||
if variant.compare_at_price > prod_compare_at_price
|
||||
assign prod_compare_at_price = variant.compare_at_price
|
||||
endif
|
||||
endif
|
||||
else
|
||||
if prod.compare_at_price_varies or prod.price_varies
|
||||
assign varies = true
|
||||
assign amount_off_varies = true
|
||||
|
||||
assign smallest_amount_off = 0
|
||||
assign largest_amount_off = 0
|
||||
|
||||
for variant in prod.variants
|
||||
|
||||
assign variant_price = variant.metafields.app--168074346497.auto_discounted_price.value | default: variant.price
|
||||
assign variant_compare_at_price = variant.compare_at_price
|
||||
|
||||
if variant.metafields.app--168074346497.discount_type.value != nil and variant.metafields.app--168074346497.discount_type.value != "fixed" and prod.metafields.app--168074346497.discount_percentage.value > 0.01
|
||||
assign deducted_percentage = 1.0 | minus: prod.metafields.app--168074346497.discount_percentage.value
|
||||
|
||||
if variant.metafields.app--168074346497.discount_percentage.value > 0.01
|
||||
assign deducted_percentage = 1.0 | minus: variant.metafields.app--168074346497.discount_percentage.value
|
||||
endif
|
||||
|
||||
assign variant_price = variant.price | divided_by: 100.0 | times: deducted_percentage | times: 100.0 | ceil
|
||||
|
||||
assign variant_compare_at_price = variant.price
|
||||
|
||||
if variant.compare_at_price > variant_compare_at_price
|
||||
assign variant_compare_at_price = variant.compare_at_price
|
||||
endif
|
||||
endif
|
||||
|
||||
if variant_compare_at_price > variant_price
|
||||
assign variant_amount_off = variant_compare_at_price | minus: variant_price
|
||||
|
||||
if smallest_amount_off == 0 or variant_amount_off < smallest_amount_off
|
||||
assign smallest_amount_off = variant_amount_off
|
||||
endif
|
||||
|
||||
if variant_amount_off > largest_amount_off
|
||||
assign largest_amount_off = variant_amount_off
|
||||
endif
|
||||
endif
|
||||
endfor
|
||||
|
||||
if smallest_amount_off == largest_amount_off
|
||||
assign amount_off_varies = false
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
-%}
|
||||
|
||||
{% comment %}
|
||||
{% if variant %}
|
||||
{% assign prod = variant %}
|
||||
{% else %}
|
||||
{%- if prod.compare_at_price_varies or prod.price_varies -%}
|
||||
{%- assign varies = true -%}
|
||||
{%- assign amount_off_varies = true -%}
|
||||
|
||||
{% comment %}
|
||||
The price may vary but the amount off may be the same for each variant.
|
||||
Here we check to see if the amount off for each variant is the same or different.
|
||||
{% endcomment %}
|
||||
{%- assign smallest_amount_off = 0 -%}
|
||||
{%- assign largest_amount_off = 0 -%}
|
||||
{%- for variant in prod.variants -%}
|
||||
{%- if variant.compare_at_price > variant.price -%}
|
||||
{%- assign variant_amount_off = variant.compare_at_price | minus: variant.price -%}
|
||||
{%- if smallest_amount_off == 0 -%}
|
||||
{%- assign smallest_amount_off = variant_amount_off -%}
|
||||
{%- endif -%}
|
||||
{%- if variant_amount_off > largest_amount_off -%}
|
||||
{%- assign largest_amount_off = variant_amount_off -%}
|
||||
{%- endif -%}
|
||||
{%- if variant_amount_off < smallest_amount_off -%}
|
||||
{%- assign smallest_amount_off = variant_amount_off -%}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
{%- endfor -%}
|
||||
|
||||
{% comment %}
|
||||
If the smallest amount off is equal to the largest amount then the discount does not vary
|
||||
{% endcomment %}
|
||||
{%- if smallest_amount_off == largest_amount_off -%}
|
||||
{%- assign amount_off_varies = false -%}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
{% endcomment %}
|
||||
|
||||
{%- if format == 'sale_text' -%}
|
||||
{%- if prod_compare_at_price > prod_price -%}
|
||||
{{ 'products.product.on_sale' | t }}
|
||||
{%- endif -%}
|
||||
{%- else -%}
|
||||
{%- assign is_percent_format = false -%}
|
||||
{%- if format == 'percent_off' or format == 'minus_percent' or format == 'save_percent' -%}
|
||||
{%- assign is_percent_format = true -%}
|
||||
{%- endif -%}
|
||||
|
||||
{%- if varies -%}
|
||||
{%- if is_percent_format -%}
|
||||
{%- capture displayed_discount -%}
|
||||
{%- render 'get-varied-percent-discount', prod: prod -%}
|
||||
{%- endcapture -%}
|
||||
{%- else -%}
|
||||
{%- capture displayed_discount -%}
|
||||
{%- render 'get-varied-amount-discount', prod: prod -%}
|
||||
{%- endcapture -%}
|
||||
{%- endif -%}
|
||||
{%- else -%}
|
||||
{%- if is_percent_format -%}
|
||||
{%- capture displayed_discount -%}{%- render 'get-percent-discount', prod: prod -%}{%- endcapture -%}
|
||||
{%- else -%}
|
||||
{%- capture displayed_discount -%}{%- render 'get-amount-discount', prod: prod -%}{%- endcapture -%}
|
||||
{%- endif -%}
|
||||
{%- endif -%}
|
||||
{% assign displayed_discount = displayed_discount | plus: 0 -%}
|
||||
|
||||
{%- unless is_percent_format %}
|
||||
{% capture formatted_currency_discount %}{{ displayed_discount | money }}{% endcapture %}
|
||||
{% endunless -%}
|
||||
{%- if displayed_discount > 0 %}
|
||||
{%- if format == 'percent_off' -%}
|
||||
{% if amount_off_varies %}{{ 'products.product.on_sale_varied_prefix' | t }}{% endif %}
|
||||
{{ 'products.product.on_sale_format_percent_off' | t: value: displayed_discount }}
|
||||
{%- elsif format == 'minus_percent' -%}
|
||||
{% if amount_off_varies %}{{ 'products.product.on_sale_varied_prefix' | t }}{% endif %}
|
||||
{{ 'products.product.on_sale_format_minus_percent' | t: value: displayed_discount }}
|
||||
{%- elsif format == 'save_percent' -%}
|
||||
{% if amount_off_varies %}
|
||||
{{ 'products.product.on_sale_format_save_percent_varied' | t: value: displayed_discount }}
|
||||
{% else %}
|
||||
{{ 'products.product.on_sale_format_save_percent' | t: value: displayed_discount }}
|
||||
{% endif %}
|
||||
{%- elsif format == 'amount_off' -%}
|
||||
{% if amount_off_varies %}{{ 'products.product.on_sale_varied_prefix' | t }}{% endif %}
|
||||
{{ 'products.product.on_sale_format_amount_off' | t: value: formatted_currency_discount }}
|
||||
{%- elsif format == 'minus_amount' -%}
|
||||
{% if amount_off_varies %}{{ 'products.product.on_sale_varied_prefix' | t }}{% endif %}
|
||||
{{ 'products.product.on_sale_format_minus_amount' | t: value: formatted_currency_discount }}
|
||||
{%- elsif format == 'save_amount' -%}
|
||||
{% if amount_off_varies %}
|
||||
{{ 'products.product.on_sale_format_save_amount_varied' | t: value: formatted_currency_discount }}
|
||||
{% else %}
|
||||
{{ 'products.product.on_sale_format_save_amount' | t: value: formatted_currency_discount }}
|
||||
{% endif %}
|
||||
{%- endif -%}
|
||||
{% endif -%}
|
||||
{%- endif -%}
|
||||
Reference in New Issue
Block a user