Math in Liquid for working out VAT

Solved
Tourist
8 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
Shopify Expert
10010 116 1846

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.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Tourist
8 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
Shopify Partner
2569 133 430

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
New Member
1 0 0

My name is Dinesh Patel. 

0 Likes