how to show 4 digits after decimal in shopify? - shopify

{% assign dvtprice = currentvariant.price %}
{% assign dvtpriceprat = currentvariant.price | times :20 | dividedby :100 %}
Example: if product price is 20.33 , then Vat= (20.33*2)/100=4.066

I think that this was the way - change times: 20 to times: 20.00.
By the way your dividedby is wrong it should be divided_by.

This isn't possible with Shopify. The core code handles prices to two decimal places, so it will round if appropriate for embedded taxes.

example:
Input
{{ 183.3574456 | round: 4 }}
Output
183.3674

Related

Precentage calculation error in shopify code

I'm running shopify and recently found an error which is the calculation of current price and compare price.
I've been trying to figure out how to solve this problem for a week and I'm guessing its in the coding which I'm not very used to.
Down below is the code running in my website at product-badge.liquid file I've also posted a picture of the badges showing "SALE" and precentage cut.
Hope anyone could help me! Kind regards/pleb.
{% if sold_out %}
<span class="soldout-title">{{ settings.soldout_title }}</span>
{% else %}
{% if on_sale %}
{% if settings.sale_title != '' %}
<span class="sale-title">{{ settings.sale_title }}</span>
{% endif %}
{% if settings.sale_percent_enable %}
<span class="percent-count">-{{ product.selected_or_first_available_variant.compare_at_price | minus: product.selected_or_first_available_variant.price | times: 100.0 | divided_by: product.selected_or_first_available_variant.compare_at_price | money_without_currency | replace: ',', '.' | times: 100 | remove: '.0'}}%</span>
{% endif %}
{% endif %}
Picture of error
I've tried your code on my test store and it works, of course, the only part I tested is the one you mentioned that is meant to display the discount percentage so the following:
<span class="percent-count">-{{ product.selected_or_first_available_variant.compare_at_price | minus: product.selected_or_first_available_variant.price | times: 100.0 | divided_by: product.selected_or_first_available_variant.compare_at_price | money_without_currency | replace: ',', '.' | times: 100 | remove: '.0'}}%</span>
First off, let's take advantage of liquid variables to make this readable and the result is the following:
{% assign price = product.selected_or_first_available_variant.price %}
{% assign compare_at_price = product.selected_or_first_available_variant.compare_at_price %}
{% assign discount_percentage = compare_at_price
| minus: price
| times: 100.0
| divided_by: compare_at_price
%}
<span class="percent-count">-{{ discount_percentage | round: 0 }}%</span>
I got rid of some filters since:
The money_without_currency filter is not needed (the discount percentage is a ratio, not a currency)
The subsequent formatting like using comma rather than dot should be done at output time but in this case, it's just better to use the round filter as suggested by Dave (I've set the round at 0 explicitly but if you want no decimal part just omit the parameter or change it if you want the decimals)
What is causing the error
This code works as long as you have the values of price and compare_at_price so if it still doesn't work just check the value of these variables with the following code (remove it after checking with the javascript console, it's just for debugging purposes):
<script>
console.log({{ product | JSON }});
console.log("Price: "+ {{ price }});
console.log("Compate_at_price: "+ {{ compare_at_price }});
</script>
Make sure to paste it after the snippet I previously inserted, then check with F12 on Chrome.

How to hide variable operation in shopify liquid

Currently on a shopify snippet I have the following tag:
{% assign totalQty = 0 | plus: 0 %}
{% for cartItem in cart.items %}
{% if item.product.tags contains 'Work-Hats' %}
{{ totalQty | plus: cartItem.quantity }}
Here I'm doing some addition but I don't want the totalQty to display yet until I'm done compiling a total.
I don't want it to display before I do the operations. How can I accomplish this?
A quantity is always a number. How could it be anything else? It cannot be a decimal, it has to be a whole number. And hopefully, greater than zero, as one cannot have zero of something in their cart.

Money filter giving worng asnwer in calculations liquid shopify

I am tryign to display amount after discount with my product. It works fine in calculation
For example my total price is 500 and discoutn is of 50% i get my answer 250 which is correct but when i use money filter to display currency unit i.e(Rs. ) it gives wrong answer which is Rs. 3
My code that gives correct output but without currency unit
{% assign percent_calculated = section.settings.custom_discount_title | times: productprice | divided_by: 100 %}
<span href="https://wdtcv.myshopify.com/discount/discount%2520code%2520promo" style="color:#FFDAB9" >{{ productprice |minus : percent_calculated }} </span></div>
OUTPUT
250
I want to display it as Rs. 250 but to achieve that i use "|money " filter and get wrong answer
{% assign percent_calculated = section.settings.custom_discount_title | times: productprice | divided_by: 100 %}
<span href="https://wdtcv.myshopify.com/discount/discount%2520code%2520promo" style="color:#FFDAB9" >{{ productprice |minus : percent_calculated | money }} </span></div>
OUTPUT
Rs. 3 (answer should be Rs. 250)
I got the solution which was to remove money_without_currency filter
as I used it because I was confused that liquid language requires data type casting you I used it a bit earlier.

Calculate how much has been donated from total spent in Shopify

So I'm working on a website where the owner donates a certain percentage of every order to a charity.
At the bottom of the user's account page is a section that shows the customer how much donations has been made from all of their orders.
So essentially the customer has spent £100 total across all orders and each order has had a 2% donation.
How would I do this using Liquid?
{% assign total = customer.tota_spent %} <!-- if total spent is 100 -->
{% assign percent = 2 %} <!-- and the donation percent is 2 -->
<!-- total divided by 100 would be 1 percent, times by 2 for 2 percent. -->
{% assign donation = total | divided_by: 100 | times: percent %}
{{ donation }}<!-- outputs 2 if the total spent is 100 -->
Sum total price of all orders.. Remove decimals.. Divide by 50..
Links to help you..
https://help.shopify.com/themes/liquid/objects/order
https://help.shopify.com/themes/liquid/filters/money-filters
https://help.shopify.com/themes/liquid/filters/math-filters

Using math filters in shopify with properties gives 0 works fine with hard codded values

I have calculated the total order price before for loop validated the value by printing in output. Mentioned below is the code block for the same:
{% assign total_items_price = 0 %}
{% for line_item in line_items %}
{%assign total_items_price = total_items_price | plus:line_item.price %}
{% endfor %}
Following are the scenarios that I have implemented to further calculations:
Trying to divide the each items price by total_items_price, but the output is 0. Even if I convert it into money format result remains same:
{{ line_item.price | divided_by:total_items_price }}
{{ line_item.price | divided_by:total_items_price | money }}
I have used multiple math filters also but result is 0:
{{ line_item.price | divided_by:total_items_price | times:discounts_amount}}
{{ line_item.price | divided_by:total_items_price | times:discounts_amount | money }}
Values of variables/properties(line_item.price, total_items_price, discounts_amount) are available inside the for loop validated by printing on UI. If I use some hard coded values instead of properties/variable it gives the correct output. for ex:
{{ line_item.price | divided_by:5 }} //gives the correct output
Maybe you might use the forloop.length value. As you do not have any conditional statement inside your loop a manual indexation is not necessary.
HTH
You should try using a math filter on line_item.price.
{% assign total_items_price = 0 %}
{% for line_item in line_items %}
{% assign total_items_price = total_items_price | plus: line_item.price %}
{{ line_item.price | times: 1.0 | divided_by: total_items_price }}
{% endfor %}
Notice the "times: 1.0" filter applied after line_item.price on line 4.