Money filter giving worng asnwer in calculations liquid shopify - 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.

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.

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

how to show 4 digits after decimal in 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

shopify liquid: plus shipping cost not working

I want to multiply the subtotal price by 21% and then add the shipping costs. Because the shipping costs are variable I use the following code.
{{ order.subtotal_price | times:1.21 | plus:shipping_method.price | money }}
But the shipping costs aren't added.
The example code in the documentation states you can divide a price by product.compare_at_price so I thought it should work the same for shipping_method.price.
What am I doing wrong? Or is it even possible?
You are using the wrong object. What you're actually looking for is order.shipping_price:
{{ order.subtotal_price | times:1.21 | plus:order.shipping_price | money }}