Page 1 of 1

How do we do math in email templates with nVelocity

Posted: Mon Oct 27, 2008 12:45 pm
by AbleMods
Hey I'm trying to do this in the order shipment email template:

Code: Select all

#set( $balance = $order.Items.TotalPriceById() - $order.Payments.Total(true) )
.
.
.
$balance.ToString("C")
Balance always comes out to $ 0.00 regardless of the numbers involved. Why?

According to the nVelocity site, math should be possible. Although there is a vague reference to only working with integer values...is that the problem?

If so, how can we determine the actual order balance, including applying any payments, within an email template?

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 5:58 am
by AbleMods
Nothing? Nada?

Nobody wants an order balance on their order/shipment emails?

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 7:59 am
by mazhar
Joe enclose the equation on the right side of the = sign in double quotes. I am taking about the following part

Code: Select all

#set( $balance = $order.Items.TotalPriceById() - $order.Payments.Total(true) )
and make it look like

Code: Select all

#set( $balance = "$order.Items.TotalPriceById() - $order.Payments.Total(true)" )

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 8:23 am
by AbleMods
Doesn't work - all that does is set the variable to the literal string value and not the mathematical result.

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 9:22 am
by mazhar
You are right. I have checked and it doesn't seems that NVelocity supports advance operation like this because this operation involves the decimal calculation.

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 9:50 am
by AbleMods
...and to think I was just about to tell Mike you should get a raise :P

That's a bummer. I understand now why the .TotalPricebyId() method was created - there's no other way to accomplish math when nVelocity is involved.

Perhaps a future version will offer an .OrderBalance() method.

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 10:00 am
by mazhar
Well there could be another workaround by sending the Email manually and creating own NVelocity parameters. In this case you can perform this sort of complex calculations before sending the Email and then plug these results into parameter list by creating new parameters. You can then use them from these new variables available within NVelocity. For example in the following post i have created a new parameter as.

Code: Select all

emailTemplates[0].Parameters.Add("vendorname", orderItem.Product.Vendor.Name);
viewtopic.php?f=42&t=8571

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 4:08 pm
by Logan Rhodehamel

Code: Select all

#set( $balance = $order.Items.TotalPriceById().ToDecimal(null) - $order.Payments.Total(true).ToDecimal(null) )
$balance.ToString("C")
NVelocity doesn't want to do math with our custom decimal type (LSDecimal). But it appears to work great if you convert it to a standard decimal. The downside is you will lose the ability for multi currency exchange rates and formatting. The string display of balance("C") will appear with whatever currency format is active for the server.

If you are only using a single currency like USD, then you can probably workaround that easily enough.

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 4:28 pm
by AbleMods
:shock:

Re: How do we do math in email templates with nVelocity

Posted: Wed Oct 29, 2008 4:35 pm
by AbleMods
Works perfect, thanks! :D