Out now! Check out the Poll results: Do you have a Shopify store?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Displaying and Calculating metafields in Liquid

Displaying and Calculating metafields in Liquid

ScinteraShopy
Shopify Partner
43 5 2

Hello All. I have Product Pages (using GemPages but not relevant - can insert Liquid easily) and in a Liquid code block I wish to display my metafields :

product.metafields.custom.saleprice and/or variant.metafields.custom.var_saleprice (for variant?)

 

However no matter what I try metafields do not appear (except obvious fields like {{product.price}} )

 

{% if product.metafields.custom.onsale == true %}
<span style="color: rgb(20,20,20);font-size: 14px">{{product.metafields.custom.saleprice}}</span>
{%endif%}

 

My end goal is to be able to Calculate saleprice as 25% off the product.price

 

Any suggestions would be much appreciated. Thanks kindly,

 

Replies 2 (2)

lizk
Shopify Staff
246 58 78

Hi there 👋

You need to make sure you are access the value of the metafield.

The following should work.

{% if product.metafields.custom.onsale.value == true %}

 

To learn more visit the Shopify Help Center or the Community Blog.

ScinteraShopy
Shopify Partner
43 5 2

Thank you Lizk!

That was very helpful. Could I ask one more favour? 

 

So, product.metafields.custom.onsale.value is now fine. 🙂

 

However, no matter what I try : product.metafields.custom.saleprice.value and variant.metafields.custom.var_saleprice.value  are null. And yes, I have double checked in the Products that these have values.

----

{% if product.metafields.custom.onsale.value == true %}
<span style="color: rgb(20,20,20);font-size: 14px">{{product.metafields.custom.saleprice.value}}

</span>

{%endif%}

----

 

And Finally.. this code works 100% However my Product has Variants and when I Select a Variant it automatically updates the Variant Price on the page. However it does not automatically update (display) what should be : variant.metafields.custom.var_saleprice.

 

{% if product.metafields.custom.onsale.value == true %}

{% capture Discount %}
{{ 0.25 | times:product.price }}
{% endcapture %}

{% capture Final %}
{{ product.price | minus:Discount }}
{% endcapture %}

<span style="color: rgb(20,20,20);font-size: 22px">{{ Final | money }}</span>

{%endif%}

 

price _ discount example.jpg