From 00e4fa59ce42c2b39be4ae13c876315f61945372 Mon Sep 17 00:00:00 2001 From: soulchild Date: Wed, 12 Aug 2026 02:53:22 +0000 Subject: [PATCH] Updated theme version --- snippets/get-display-discount.liquid | 226 +++++++++++++-------------- 1 file changed, 106 insertions(+), 120 deletions(-) diff --git a/snippets/get-display-discount.liquid b/snippets/get-display-discount.liquid index ba2ad7f..f648b38 100644 --- a/snippets/get-display-discount.liquid +++ b/snippets/get-display-discount.liquid @@ -1,8 +1,15 @@ -{%- comment -%} - Pass in: +{%- doc -%} + Renders discount text. - - product or variant - - format string, one of + @param {object} prod - The product we're getting discounts for. We use 'prod' to avoid collision with the global 'product' object. + @param {object} [variant] - The variant we're getting a discount for. If a variant is supplied, that overrides the product and changes the output. + @param {string} format - The discount format. Format types are listed below. +{%- enddoc -%} + + +{%- liquid + comment + Discount format types: - "sale_text" - "percent_off" - "minus_percent" @@ -10,14 +17,27 @@ - "amount_off" - "minus_amount" - "save_amount" -{%- endcomment -%} + endcomment -{%- assign varies = false -%} + assign price_varies = false -{%- liquid - assign prod_price = prod.price + if format contains 'percent' + assign is_percent_format = true + else + assign is_percent_format = false + endif + + assign prod_price = prod.metafields.app--168074346497.min_auto_discounted_price.value | default: prod.price assign prod_compare_at_price = prod.compare_at_price + if prod_price < prod.price + assign prod_compare_at_price = prod.price + endif + + if prod.compare_at_price > prod_compare_at_price + assign prod_compare_at_price = prod.compare_at_price + endif + if variant assign prod = variant @@ -41,11 +61,13 @@ endif else if prod.compare_at_price_varies or prod.price_varies - assign varies = true - assign amount_off_varies = true + assign price_varies = true + assign discount_varies = true - assign smallest_amount_off = 0 - assign largest_amount_off = 0 + # The price may vary, but the amount off may be the same for each variant, so + # we have to check. + assign smallest_discount = 0 + assign largest_discount = 0 for variant in prod.variants @@ -69,121 +91,85 @@ 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 + if is_percent_format + assign variant_discount = variant_compare_at_price | minus: variant_price | times: 100.00 | divided_by: variant_compare_at_price | round + else + assign variant_discount = variant_compare_at_price | minus: variant_price endif - if variant_amount_off > largest_amount_off - assign largest_amount_off = variant_amount_off + if smallest_discount == 0 or variant_discount < smallest_discount + assign smallest_discount = variant_discount + endif + + if variant_discount > largest_discount + assign largest_discount = variant_discount endif endif endfor - if smallest_amount_off == largest_amount_off - assign amount_off_varies = false + # If the smallest discount is equal to the largest discount, then the discount doesn't vary + if smallest_discount == largest_discount + assign discount_varies = false endif endif endif + + if format == 'sale_text' + if prod_compare_at_price > prod_price + echo 'products.product.on_sale' | t + endif + else + capture displayed_discount + # If the price varies, we've already calculated the largest discount + if price_varies + echo largest_discount + else + if is_percent_format + render 'get-percent-discount', prod: prod + else + render 'get-amount-discount', prod: prod + endif + endif + endcapture + + # Because we possibly get this value from a snippet, we have to convert to a number + assign displayed_discount = displayed_discount | plus: 0 + + unless is_percent_format + assign formatted_currency_discount = displayed_discount | money + endunless + + if displayed_discount > 0 + case format + when 'percent_off', 'minus_percent', 'amount_off', 'minus_amount' + if discount_varies + echo 'products.product.on_sale_varied_prefix' | t + echo ' ' + endif + + assign translation_string = 'products.product.on_sale_format_[[format]]' | replace: '[[format]]', format + + if format contains 'amount' + echo translation_string | t: value: formatted_currency_discount + else + echo translation_string | t: value: displayed_discount + endif + + when 'save_percent', 'save_amount' + capture translation_string + if discount_varies + echo 'products.product.on_sale_format_[[format]]_varied' | replace: '[[format]]', format + else + echo 'products.product.on_sale_format_[[format]]' | replace: '[[format]]', format + endif + endcapture + + if format contains 'amount' + echo translation_string | t: value: formatted_currency_discount + else + echo translation_string | t: value: displayed_discount + endif + endcase + 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 -%}