Liquid divided_by: Divide Number in Shopify
Drive 20-40% of your revenue with Avada
Shopify store owners can leverage Liquid, a powerful and free templating language, to enhance their store's functionality and appearance. Liquid enables dynamic data display and customization, making it an essential tool for creating a professional and user-friendly storefront.
This article will focus on the divided_by filter, a useful Liquid math filter for performing calculations within your Shopify theme.
What is Liquid divided_by?
Liquid divided_by is a Shopify filter used to divide numbers within your store's theme code. This is particularly helpful for calculations like discounts, averages, or displaying quantities per unit. It works within output tags and uses the "|" symbol to denote the filter. Keep in mind that when dividing by an integer, the result is also an integer (rounded down). For decimal precision, use a float as the divisor.
Understanding divided_by
It is possible for the Shopify users to load the dynamic data from the texting, numbers, etc. With the math filters, they can manage a number of these store data in the dashboard. Thank the valuable asset as Liquid math filters, the online merchants can apply the math functions into their store’s websites to display customer’s information and other data. Therefore, math filters are effective ways to boost the customer’s conversions in their stores. Moreover, it is also a powerful tool such as sales, promotions, etc.
Especially, the Shopify store owners can link these math filters with any other filters. The form of these are in an output tag `` and then they are denoted by |
, which are in order from the left side to right side. In math filters, there are many ways to deal with the outputs as a number. One of the features of this math filters is divided_by
which allows the Shopify store owners to divide by an integer and an integer will be a result of this chain. In case online sellers divide the float, the results will be a float. Or if the user divides a number by the specified number, the output will be the nearest integer.
Basic Syntax
{{ number1 | divided_by: number2 }}
Code snippet:
- number1 is the number you want to divide.
- number2 is the divisor (the number you're dividing by).
Example:
Let's say you have a product priced at $20 and you want to offer a 50% discount.
Code snippet:
{% assign original_price = 20 %}
{% assign discount_percentage = 50 %}
{% assign discount_amount = original_price | times: discount_percentage | divided_by: 100 %}
{{ discount_amount }} // Output: 10
Example Usage in Shopify Themes:
- Displaying the price per item in a multi-pack:
Code snippet:
{{ product.price | divided_by: product.metafields.my_fields.quantity_per_pack }}
- Calculating the percentage discount on a sale item:
Code snippet:
{% assign savings = product.compare_at_price | minus: product.price %}
{% assign discount_percentage = savings | times: 100.0 | divided_by: product.compare_at_price %}
You save {{ discount_percentage | round }}%!
How to set Liquid divided_by?
Step 1: Input
<!-- product.price = 200 -->
{{ product.price | divided_by: 10 }}
Input<!-- product.price = 200 -->0 |
Step 2: Output
20
After entering the input, the output will be as below:
Output20 |
In conclusion
This “Liquid divided_by: Divide Number in Shopify” article provides the readers with the most basic steps to divide a number in this Shopify marketplace. Moreover, we hope that online sellers can approach Liquid math filters more by our chain of articles like the following list: