All things Shopify and commerce
Hi everyone,
I'd like to insert a math formula as metafield.
Basically I'd like to have the product price divided by 4 in a dynamic way so the results shown is updated based on the price that may change.
I found something like that
{{product.price | divided_by: 4.0}}
but
1. I'm not able to insert this as metafield
2. the comma is at the wrong place (22,90€ / 4 = 5,72€ while the result I get back is 572.5)
Anyone that can help?
Thanks in advance!
To show the correct decimals in your formula, you just need to add the money filter, so basically change your code to: {{product.price | divided_by: 4.0 | money}}
Is there any reason why you need to use metafields? Seems like just a simple formula that you can add to the product page directly using custom liquid block. I have not tried using liquid directly in a metafield, but if you need to make it dynamic, you could consider inserting the variable itself, i.e. if the divided by 4 changes by product, then insert that as a variable in your metafield, and for your formula, just get the custom liquid code to read that variable and apply to the formula accordingly.
thanks for the feedback! I'd want to show this calculation for certain products only so it would be easier manage this through metafield directly..
The formula as you suggested works but I can't make it work as metafield value 😕
I don't think the metafields will render liquid code based on the documentation: https://shopify.dev/apps/custom-data/metafields/types
Maybe others know more about how you can make it work.
Personally i still don't see why need to have the liquid formula in the metafield, as using a custom liquid block would serve the same purpose. Instead of needing the liquid code, you add a flag / variable (i.e. 4) in your metafield, and then your liquid code just need to check if the metafield has value or not for this product. If it does, then do the division, if it doesn't, then do nothing.
thanks for the feedback! When you say "You can define a Liquid variable in your metafield value with the formula" which type of metafield you would suggest?
I got the formula now, but I'm not understanding which type of metafield use to make it work 😕
I'm looking to do something similar: show a per unit price for boxes of beer .
This requires I can take the variant price (e.g. 12 pack price) and divide by the number of units (variant metafield number).
Can anyone help with the liquid code for variant price and how I display that divided by another variant metafield?
This can be done via coding
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024