From 293566f1b4936536fa9bb61d00c6ae2e6bbcad29 Mon Sep 17 00:00:00 2001 From: Axel Kee Date: Fri, 29 Aug 2025 15:39:06 +0800 Subject: [PATCH] Add discount price calculation --- product-card.liquid | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/product-card.liquid b/product-card.liquid index 841b078..0d93063 100644 --- a/product-card.liquid +++ b/product-card.liquid @@ -123,7 +123,19 @@ Supported variables {%- if show_prices and product.template_suffix != 'quote' -%}
- {{ product.selected_or_first_available_variant.price | money }} + {% assign v = product.selected_or_first_available_variant %} + {% assign variant_price = product.selected_or_first_available_variant.price %} + {% if v.metafields.app--168074346497.discount_type.value != nil and v.metafields.app--168074346497.discount_type.value != "fixed" and product.metafields.app--168074346497.discount_percentage.value > 0.01 %} + {% assign deducted_percentage = 1.0 | minus: product.metafields.app--168074346497.discount_percentage.value %} + + {% if v.metafields.app--168074346497.discount_percentage.value > 0.01 %} + {% assign deducted_percentage = 1.0 | minus: v.metafields.app--168074346497.discount_percentage.value %} + {% endif %} + + {% assign variant_price = v.price | divided_by: 100.0 | times: deducted_percentage | times: 100.0 | ceil %} + {% endif %} + + {{ variant_price | money }}
{%- endif -%} @@ -277,11 +289,16 @@ Supported variables // Update hidden input with the correct variant ID $('#selected-variant-id-{{ product.id }}').val(selectedVariant.id); + {% assign deducted_percentage = 1.0 %} + {% if product.metafields.app--168074346497.discount_percentage.value > 0.01 %} + {% assign deducted_percentage = 1.0 | minus: product.metafields.app--168074346497.discount_percentage.value %} + {% endif %} + // Update displayed price let locale = Shopify.locale; let currency = Shopify.currency.active; let country = Shopify.country; - let amount = new Intl.NumberFormat(`${locale}-${country}`, { style: "currency", currency: currency }).format(selectedVariant.price / 100 ); + let amount = new Intl.NumberFormat(`${locale}-${country}`, { style: "currency", currency: currency }).format(selectedVariant.price / 100 * {{ deducted_percentage }}); $(this).find('.product-price').text(amount); } else { console.log('No matching variant found for the selected options.');