Math in Liquid for working out VAT

Solved
cdevlin
Tourist
12 0 0

Hi, hope someone can help.

 

I'm looking to breakdown the Cart total to show VAT (taxes) for the products in the cart.

So basically I want to take the cart total divide by  1.2 to then equal (output) the VAT free total.

So I know this: 

{{ cart.total_price | divided_by: 1.2 | money }}

will get me the VAT free total.

Then I want to take the cart total again and then minus the VAT free total from that to get me to my final VAT of the products in cart. But my question is how do I assign the output of the first math equation to then allow me to use that in a formula like:

{{ cart.total_price | minus: VATFreeTotal | money }}

to then achieve this final result.

 

For a better understanding, lets say the total is £150:

150 / 1.2 = 125

150 - 125 = 25

25  being the VAT of products.

 

Am I looking at this in the right way?

 

Thanks for any help.

0 Likes
Jason
Shopify Expert
10360 158 2003

This is an accepted solution.

Assuming I am following what you need, look at using assign

 

{% assign VATFreeTotal = cart.total_price | divided_by: 1.2 %}

That will give you the VATFreeTotal variable to use in your code further down the page. 

This does make the assumption that your formula is correct for what you need. I've not considered that, just the Liquid to make a variable.

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
cdevlin
Tourist
12 0 0

Ahh that has solved my issue perfectly!

I was putting the last section in the curly brackets like below, hence not getting the results I wanted.  

{% assign VATFreeTotal = {{cart.total_price | divided_by: 1.2}} %}

 

Also, do you know how to stop the outputted numbers from being rounded up? for example: 

636.56 / 1.2 = 530.4666666

but this gets rounded 530.47 in Shopify, that then knocks out the calculations that follow on (not by much to be fair)

 

EDIT: As I come to think of it the rounding up probably won't massively be an issue so I'll leave as is.

 

Thanks for your help.

0 Likes
PaulNewton
Shopify Partner
2581 136 471

To assign an output to a variable use liquids assign tag:

{% assign VATFreeTotal = cart.total_price | divided_by: 1.2 %}
{{ cart.total_price | minus: VATFreeTotal }}

 

One liner Vat Free total for display:

{{ cart.total_price | divided_by: 1.2 | minus: cart.total_price | times:-1 | money }}

times a negative (-1) reverses the sign when dividing a smaller number by a larger number.

 

Note: Keep in mind using formatting filters like the '| money' filter may put non-numerical characters into the numbers.

So it can be better to ommit them until ready to output currency .

Which can lead to confusing results that may be hard to diagnose. Of course not using it means your dealing with more than 2 decimal places.

 

Money filter by default will do rounding.

Use round filter with specific decimal places to control rounding it's an issue , otherwise it rounds to the closest integer

{{ 3.14159 | round: 2 }}
3.14
{{ 3.14159 | round: 5 }}
3.14159

 

 

 

 

 

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
0 Likes
Dinesh960
New Member
1 0 0

My name is Dinesh Patel. 

0 Likes
CatherineBrag
New Member
3 0 0

It's not easy to calculate. And all the more so for a person like me, because I never even understand the basics of math. I always needed https://plainmath.net/post-secondary to read before I started to do assignments. Look here for how to do these calculations, you might find the answer there!

0 Likes