Liquid code not returning expected values. Calculating a Sale Price on Variants

Hello friends. Both Google Gemini Pro and I cannot solve this issue. :wink:

  1. I use GemPages to edit my Product pages. (not highly relevant)
  2. I have 2 metafields that I need to use: variant.metafields.custom.var_onsale (true/false) and variant.metafields.custom.var_percentage (Integer).
  3. On my Product pages (templates) I add a Liquid code to achieve 1 thing. The Sale Price. This liquid code should calculate that value very simply. But it does not. Am I missing something? (Is Google Gemini Pro missing something?)
  4. This is only needed on Products with multiple Variants, and ideally, I need the Sale Price for the selected Variant is shown.
  5. I have verified the data in the metafields is valid and present.

Sample Output:

  • on_sale = ‘true’
  • discount_percentage = i.e. 25
  • original_price (variant.price) = i.e. 80.31
  • discount_amount is broken down to small parts on purpose. It still returns 0.0 both times. Should be 20.08
  • and sale_price = original_price (i.e. 8031) - wrong. Should be 60.23 (hand calculated)
{% for variant in product.variants %}
  {% assign on_sale = variant.metafields.custom.var_onsale %}
  {% assign discount_percentage = variant.metafields.custom.var_percentage.value | append: '' | plus: 0 %}

  Raw percentage value: {{ variant.metafields.custom.var_percentage.value }} 

  {% if variant.metafields.custom.var_onsale %}
   {% assign original_price = variant.price %}
   
  {% assign discount_amount = discount_percentage.0 | times: 0.01 %} 
  {{discount_amount}} - 
  {% assign discount_amount = original_price | times: discount_amount %} 
  {{discount_amount}} - 
  {% assign sale_price = original_price | minus: discount_amount %} 
  {{sale_price}}
  Sale Price: {{ sale_price }}
  {% else %}
    {% assign sale_price = variant.price %} 
    Original Sale Price: {{ sale_price }}
  {% endif %}
  
  <div class="product-price" data-variant-id="{{ variant.id }}">
    {% if sale_price != variant.price %}
      <span class="original-price">{{ variant.price | money }}</span>
      <span class="sale-price">{{ sale_price | money }}</span>  
    {% else %}
      <span class="price">{{ sale_price | money }}</span>
    {% endif %}
  </div>
  <script>// Inside the updatePrice function
console.log("Metafield value:", variant.metafields.custom.var_percentage.value);
console.log("Type:", typeof variant.metafields.custom.var_percentage.value);
</script>
{% endfor %}
<div id="product-price-container"> 
  </div>
  

Hello @ScinteraShopy

Your code has an error in this line:

  {% assign discount_amount = discount_percentage.0 | times: 0.01 %} 

It must be:

  {% assign discount_amount = discount_percentage | times: 0.01 %} 

I tried

Result

GemPages_2-1710908158855.png

Please try it and let me know the results

Best regards,
GemPages Support Team

Hi awesome GemPages support team. Thanks for your reply and suggestion. I have since revised my Liquid code and this is how it looks with results shown below the code: (with primitive Liquid debugging).

{% for variant in product.variants %}
  {% assign on_sale = variant.metafields.custom.var_onsale %}
  {% assign discount_percentage = variant.metafields.custom.var_percent.value %}

  {% if variant.metafields.custom.var_onsale %}
    {% assign original_price = variant.price %}

    On Sale: {{on_sale}} --
    Original Price: {{original_price}} -- 
    Disc Percent: {{discount_percentage}} -- 

    {% assign discount_amount = original_price | times: (discount_percentage / 100.0) | round: 2 %} 
    Disc Amt: {{ discount_amount }} -- 

    {% assign sale_price = original_price | minus: discount_amount | round: 2 %}
    Sale Price: {{ sale_price }} -- 

    Sale Price: {{ sale_price | divided_by: 100.0 | money }}
  {% else %}
    {% assign sale_price = variant.price %}
    Original Sale Price: {{ sale_price | money }}
  {% endif %}

{% endfor %}

Output:

On Sale: true

Original Price: 8031 (80.31)

Disc Percent: 25.0 (25%)

Disc Amt: 200775.0 (should be 20.08)

Sale Price: -192744.0 (should be 60.23)

Sale Price: $-19.27

$80.31$-19.27

Thank you!

Edward.

Problem solved. Thank you.

I’m getting an error on this line:

{% assign discount_amount = original_price | times: (discount_percentage / 100.0) | round: 2 %}

"Unexpected character / in “{{original_price | times: (discount_percentage / 100.0) | round: 2 }}”