Liquid Math

I trying to get a price to display based on some liquid math in a custom liquid block. I’m new to liquid coding.

I want something like
{{ product.price | times: product.quantity }} divided_by {{ product.quantity | times: product.metafields.custom.bundle_qty }} = Price per unit

The full product price is based on a bundle quantity that will be different on each product so I have set up a metafield for this bundle quantity value.

On a related note when I display {{ product.price }} it shows 291 instead of $2.91.

Hi @thebrandhub

May this article could help you

https://shopify.dev/docs/api/liquid/filters/math-filters

1 Like

There are two things in your question:

  1. use math

  2. display price

for 1 you can use math filters from liquid api

for 2 you can use money filter to show price with currency.[more details] product.price represents amount in cents. so consider this when you perform any math operations with it. Also when you do math operations make sure that the datatype of all the values is number.

{{ product.price | money }}
{{ product.price | money_with_currency }}

In your example it looks like product.quantity is redundant. you can directly divide {{ product.price | divided_by: product.metafields.custom.bundle_qty }}

1 Like

Hi,

You need to use this code to display price with currency sign

{% assign total_price = product.price | times: product.quantity %}
{% assign bundle_qty = product.metafields.custom.bundle_qty %}
{% assign price_per_unit = total_price | divided_by: (product.quantity | times: bundle_qty) %}

{{ price_per_unit | money }}
1 Like

I’ve just realised i haven’t referred to the correct product quantity. How do I select the quantity that the customer is selecting, for example, they want to buy 10 boxes.