How do we do math in email templates with nVelocity

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

How do we do math in email templates with nVelocity

Post by AbleMods » Mon Oct 27, 2008 12:45 pm

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?
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

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

Post by AbleMods » Wed Oct 29, 2008 5:58 am

Nothing? Nada?

Nobody wants an order balance on their order/shipment emails?
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

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

Post by mazhar » Wed Oct 29, 2008 7:59 am

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)" )

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

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

Post by AbleMods » Wed Oct 29, 2008 8:23 am

Doesn't work - all that does is set the variable to the literal string value and not the mathematical result.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

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

Post by mazhar » Wed Oct 29, 2008 9:22 am

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.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

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

Post by AbleMods » Wed Oct 29, 2008 9:50 am

...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.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

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

Post by mazhar » Wed Oct 29, 2008 10:00 am

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

User avatar
Logan Rhodehamel
Developer
Developer
Posts: 4116
Joined: Wed Dec 10, 2003 5:26 pm

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

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

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.
Cheers,
Logan
Image.com

If I do not respond to an unsolicited private message, it's not because I'm ignoring you. It's because the answer to your question is valuable to others. Try the new topic button.

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

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

Post by AbleMods » Wed Oct 29, 2008 4:28 pm

:shock:
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

User avatar
AbleMods
Master Yoda
Master Yoda
Posts: 5170
Joined: Wed Sep 26, 2007 5:47 am
Location: Fort Myers, Florida USA

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

Post by AbleMods » Wed Oct 29, 2008 4:35 pm

Works perfect, thanks! :D
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com

Post Reply