smarty passing the value from {math} equatio to a variable

Noel Balaba picture Noel Balaba · Jun 22, 2012 · Viewed 8.4k times · Source

Have a nice day everyone, I have something to ask your hel, to better understand here is my code:

{math equation=((($order_total-$commission)+$discount+$delivery_charge)*$rate)}

I want that to be pass to another variable,in php I want to be like this

<?php 
  $var=0
  foreach($result as $resultA){
   $var = $var+((($order_total-$commission)+$discount+$delivery_charge)*$rate);
}
?>

Hope you can help me guys!

Answer

rodneyrehm picture rodneyrehm · Jun 22, 2012

If you're using Smarty 3 I highly recommend ditching the {math}:

{$order_total = 123}
{$commission = 13}
{$discount = 10}
{$delivery_charge = 20}
{$rate = 1.1}

{$result = 0}
{$result = $result + ($order_total - $commission + $discount + $delivery_charge) * $rate}
{$result}

It is both better readable and faster (as the expression is actually compiled, rather than eval()ed over and over again).


Smarty 2 equivalent:

{assign var="order_total" value=123}
{assign var="commission" value=13}
{assign var="discount" value=10}
{assign var="delivery_charge" value=20}
{assign var="rate" value=1.1}

{assign var="result" value=0}
{math 
  assign="result"
  equation="result + (order_total - commission + discount + delivery_charge) * rate"
  result=$result
  order_total=$order_total
  commission=$commission
  discount=$discount
  delivery_charge=$delivery_charge
  rate=$rate
}
{$result}

If there is any chance to upgrade to Smarty 3 - do it!