Liquid maths: calculating a percent discount in the order notification email template - shopify

Why is this returning 0?
{% if discounts %}
Discount
{{ discounts_amount | divided_by: subtotal_price | times: 100 }}%
{% endif %}
I have also tried accommodating integer maths, but no luck.

Wrong syntax. Try this
{% if discounts %}
Discount {{ discounts.amount | divided_by: subtotal_price | times: 100 }}%
{% endif %}

It may have been a long time but I'm still replying because of the many views.
We have to convert integer to float
{% assign my_integer = 7 %}
{% assign my_float = my_integer | times: 1.0 %}
{{ 20 | divided_by: my_float }}
https://shopify.github.io/liquid/filters/divided_by/

Related

Liquid coding (Shopify) "and" and "elsif" not functioning together

I'm new to liquid coding while starting my own Shopify store (although I had some experience with coding). I'm currently stuck trying to get this code to function. I've added 3 text options in the liquid file for each variable. The idea is that when the criteria are met, it would display the appropriate text option. Please see the code string here for ref.
{% if section.settings.free_shipping_announcement_bar %}
{% assign promote_text = section.settings.promote_free_shipping_text | escape %}
{% assign unlocked_text = section.settings.unlocked_free_shipping_text | escape %}
{% assign unlocked_del_text = section.settings.unlocked_free_delivery_text | escape %}
{% assign threshold = section.settings.free_shipping_threshold | times: 100 %}
{% assign value_left = threshold | minus:cart.total_price %}
{% assign value_left_money = value_left | money %}
<div class="announcement-bar">
{% if value_left <= 0 %}
<p class="announcement-bar__message">{{unlocked_text}}</p>
{% elsif value_left >= 1 and value_left < 50 %}
<p class="announcement-bar__message">{{unlocked_del_text}}</p>
{% else %}
<p class="announcement-bar__message">{{promote_text | replace:'(value)' , value_left_money}}</p>
{% endif %}
basically, the idea is if the cart total is between $0-$99 I will show the "promote_text", if it's between $100-$149 it will show the "Unlocked_free_del_text"(free delivery), then at $150+ in the cart it shows "unlocked_text".
It currently shows the "promote_text" from $0-$149 then changes directly to the "unlocked_text" and skips the middle section for "unlocked_del_text"
my best assumption is that I'm using the elsif function wrong but I've trailed as many adjustments as I can think of without breaking the code, so any advice would be greatly appreciated!
Update:
After checking and testing the code over dev store I find you need to use divided_by to value_left value to compare with the value into normalized format.
so your code needs an update to this line
{% assign value_left = threshold | minus:cart.total_price | divided_by: 100%}
becase case is multile of 100 to process to cart value and need to normalized before test into if else condition.
The code looks great, I am not sure why this not works properly, you can check and try in this way also and I hope it will work. overwise you need to post the whole code along with schema and anyone replicate it dev store and find the solution.
{% if section.settings.free_shipping_announcement_bar %}
{% assign promote_text = section.settings.promote_free_shipping_text | escape %}
{% assign unlocked_text = section.settings.unlocked_free_shipping_text | escape %}
{% assign unlocked_del_text = section.settings.unlocked_free_delivery_text | escape %}
{% assign threshold = section.settings.free_shipping_threshold | times: 100 %}
{% assign value_left = threshold | minus:cart.total_price | divided_by: 100%}
{% assign value_left_money = value_left | money %}
<div class="announcement-bar">
{% if value_left <= 0 %}
<p class="announcement-bar__message">{{unlocked_text}}</p>
{% elsif value_left >= 1 and value_left < 50 %}
<p class="announcement-bar__message">{{unlocked_del_text}}</p>
{% else %}
<p class="announcement-bar__message">{{promote_text | replace:'(value)' , value_left_money}}</p>
{% endif %}
</div>
{% endif %}
Thanks for the quick response! I trialed the snippet you added but unfortunately ran into the same issue. here is the schema that I added to impact the section of code I input. If you are able to spot a simple issue in here please let me know, otherwise, I will continue to troubleshoot personally and hopefully find a solution or alternate method, to avoid troubling someone with having to replicate the entire code to recreate the issue.
{
"type":"checkbox",
"label":"enable free shipping bar",
"id":"free_shipping_announcement_bar",
"default":false
},
{
"type":"text",
"id":"promote_free_shipping_text",
"label":"message to promote free shipping"
},
{
"type":"text",
"id":"unlocked_free_shipping_text",
"label":"message for unlocked free shipping"
},
{
"type":"text",
"id":"unlocked_free_delivery_text",
"label":"message for unlocked free delivery"
},
{
"type":"range",
"id":"free_shipping_threshold",
"label":"threshold for free shipping",
"min":0,
"max":200,
"step":5,
"unit":"$",
"default":150
}

How to indicate "New" sign for fresh product on liquid

I'm using liquid on Shopify. And new to liquid.
Now, I'm trying to indicate "new" sign for products within 7 days.
{% assign today_date = 'now' | date: '%s' %}
{% assign create_date = product.created_at | date: '%s' %}
{% assign dif = today_date - create_date %}
<div>Time diff is {{ dif }}</div>
{% if dif < 30000 %}
<div>New</div>
{% endif %}
It shows errors like this.
Time diff is 1607714358
Liquid error: comparison of String with 30000 failed
What should I do?
Thanks in advance.
All liquid operations (+,-,/,*/%) are done via filters.
So this one here {% assign dif = today_date - create_date %} is incorrect.
It should be like so {% assign dif = today_date | minus: create_date %}.
This is your only mistake in the code.
Final code should be:
{% assign today_date = 'now' | date: '%s' %}
{% assign create_date = product.created_at | date: '%s' %}
{% assign dif = today_date | minus: create_date %}
<div>Time diff is {{ dif }}</div>
{% if dif < 30000 %}
<div>New</div>
{% endif %}

donation counter on starting page

I need a donation counter (total sum), which shows on the starting page and on a specific page. My problem here is, that I included a snippet to get the total donation and these donations come from one specific product. I managed to get to get the total sum of donations and I can display it on the product page. But I want to display it on the startpage and on a specific page. I wrote a product-sold-count.liquid as following:
{% assign productStartCount = product.metafields.stock.initial | times:1 %}
{% if productStartCount > 0 %}
{% assign productInventory = product.variants.first.inventory_quantity %}
{% assign totalSum = productStartCount | minus:productInventory | times: product.price | times: product.metafields.donation.percent | divided_by: 10000 %}
<p>{{ totalSum }} € were donated until now!</p>
{% endif %}
Now I want to get this totalSum to another page or more likely to the start page and to other pages? I tried: {% include 'product-sold-count' %} on the specific liquids like page.liquid or theme.liquid but that won't work. How can I achieve this?
The problem is on product page, you have the variable product. But on other pages, this variable is longer valid.
In order to have total count for all product, you can repeat the above code for each product and get the sum of all products. Note that Shopify provide all_products variable, but limit to 20 products per page. You can use collection.products if it's on a collection (or, assign all donation products to donation collection then get all products of this collection)
{% assign allProductCount = 0 %}
{% for product in all_products %}
{% assign productStartCount = product.metafields.stock.initial | times:1 %}
{% if productStartCount > 0 %}
{% assign productInventory = product.variants.first.inventory_quantity %}
{% assign totalSum = productStartCount | minus:productInventory | times:
product.price | times: product.metafields.donation.percent | divided_by: 10000 %}
{% assign allProductCount = allProductCount | plus: totalSum %}
{% endif %}
{% endfor %}
<p>{{ allProductCount }} € were donated until now!</p>

Shopify product.price.liquid Math Filters not displayed correct

I've added some code in the product.price.liquid to make a simple math formula:
<span id="extrainfo">
<span id="perkg">{{ variant.price | times: 100.0 | divided_by: variant.weight | money }}</span>/100g
</span>
Whole Code of the liquid here
Result: On the Product detail page, the result of the math formula is shown right=> Detail Page, but on the collections page or homepage, there comes "Inf"=> Collection Page
Any Ideas?
The last | money parameter might be confusing. Try like this
{% assign variant_price = variant.price | times: 100.0 %}
{% assign variant_price = variant_price | divided_by: variant.weight %}
{% assign variant_price | money %}
{{ variant_price }}

Shopify: Order Print App is not picking up variables

I am trying to display a Final Sale message on order receipts for products that are 60% off. This bit of code does display the message on the individual product page, but when I insert in the template I'm using in the Order Printer app, the message does not seem to display.
I've contacted various Shopify support people, but they have not been able to identify the problem. Here's the code I'm inserting:
<!--if item is 60% off, it displays message: -->
{% if product.compare_at_price %}
{% assign sixtyPercentOff = product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price | round %}
{% if sixtyPercentOff == 60 %}
<p style="color: #B21F1F;">
This item is final sale -- no returns or exchanges are accepted.
</p>
{% endif %}
{% endif %}
Is it because Order Printer does not recognize variables such as "compare_at_price"?
product.compare_at_price doesn't exist.
For product you have: compare_at_price_max, compare_at_price_min, compare_at_price_varies
Do you mean to use something like: {% if product.compare_at_price_min > 0 %}
Or if you have variants you can use directly variant.compare_at_price
For first available variant of product: {% if product.variants[0].compare_at_price > 0 %}