Smarty total amount sum - php

how can I count the final price to pay if I have the following code
$Total
:
{foreach from=$items key=k item=item}
<tr>
<td class="align_left"><strong>{$item.Title}</strong></td>
<td><img src="products_images/{$item.ImagePath}" width="60" height="60" alt="{$item.Title}" /></td>
<td>{$item.quantity}</td>
<td>{$item.Price}<span class="s_currency s_after"> лв.</span></td>
<td>{math equation="x * y" x=$item.Price y=$item.quantity}<span class="s_currency s_after"> лв.</span></td>
</tr>
{/foreach}
<tr class="last">
<td class="align_right" colspan="4"><strong>Всичко:</strong></td>
<td class="s_secondary_color"><span class="s_currency s_before">$</span>{$Total}</td>
</tr>

I did not understand why you need it so, but do it:
NOTICE: for Smarty 3.
{$Total = 0 }
{foreach from=$items key=k item=item}
<tr>
<td class="align_left"><strong>{$item.Title}</strong></td>
<td><img src="products_images/{$item.ImagePath}" width="60" height="60" alt="{$item.Title}" /></td>
<td>{$item.quantity}</td>
<td>{$item.Price}<span class="s_currency s_after"> лв.</span></td>
<td>{math equation="x * y" x=$item.Price y=$item.quantity}<span class="s_currency s_after"> лв.</span></td>
{$Total = $Total+ ($item.Price*$item.quantity) }
</tr>
{/foreach}
<tr class="last">
<td class="align_right" colspan="4"><strong>Всичко:</strong></td>
<td class="s_secondary_color"><span class="s_currency s_before">$</span>{$Total}</td>
</tr>

Better to use MySQL query with GROUP BY and SUM() if individual amounts store in DB

use assign in ur loop and add the values to variable {$total}
{assign var=total value=0}
better loop through ur array in php and calculate total, if data coming from database, then query sum

Related

How display the first element of a list ? Smarty, Prestashop module

I'm trying to change a view in a Prestashop Module. I would like to display just the first one case of a list.
Here, is the original list:
{if count($tax_details)>0 && $use_taxes}
<br />
<br />
<table cellpadding="3">
<thead>
<tr style="color:#FFFFFF; background-color: #4D4D4D; font-weight:bold; padding:2px;">
<th style="width: 40%;">{l s='TAX DETAILS' mod='opartdevis'}</th>
<th style="width: 20%;">{l s='Tax rate' mod='opartdevis'}</th>
<th style="width: 20%;">{l s='Total without tax' mod='opartdevis'}</th>
<th style="width: 20%;text-align: right;">{l s='Total tax' mod='opartdevis'}</th>
</tr>
</thead>
{foreach $tax_details as $tax}
<tr>
<td style="width: 40%;">{$tax.prefix|escape:'htmlall':'UTF-8'}</td>
<td style="width: 20%;">{$tax.name|escape:'htmlall':'UTF-8'}</td>
<td style="width: 20%;">{Tools::displayPrice($tax.total_ht)}</td>
<td style="width: 20%;text-align: right;">{Tools::displayPrice($tax.total_tax)}</td>
</tr>
{/foreach}
</table>
{/if}
It display this :
And I would like to display just this number in yellow on another place.
This is my code (it situated on the same page than the original list):
{if count($tax_details)>0}
<tr>
<td colspan="7" style="text-align:right;">
Total éco taxes :
</td>
<td colspan="1" style="text-align:right;">
<span id="total_price_remiseincl">
{Tools::displayPrice($tax_details[0].total_tax)}
</span>
</td>
</tr>
{/if}
I have this error:
I tried many ways to write it, but I don't find how to do.
Could you help me pls ? I'm new with Smarty.
Thanks in advance
Malaury
You need to set up a loop again
{assign 't' '0'}
{foreach $tax_details as $tax}
{if $t === '0'}
{Tools::displayPrice($tax.total_tax)}
{assign 't' '1'}
{/if}
{/foreach}
Alternatively, you can use the 'first' properly like below. (docs)
In your foreach, use,
{if $smarty.foreach.tax_details.first}
<tr>
<td colspan="3"></td>
<td style="width: 20%;text-align: right;">
{Tools::displayPrice($tax.total_tax)}
</td>
</tr>
{/if}

Smarty new table row every nth time

Hi I'm making this HTML email and thanks to the good old Outlook 07 that adds a really nice page break I kinda have to change my logic. Currently my html looks something like this:
<table>
<tr> //I want for every 6th element to create a new <tr>
<td>
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{/foreach}
</table>
</td>
</tr>
</table>
Right now after the 6th element it creates this page break so I want is when I have let's say 12 elements each six to have their own table row with table inside.
The following code should work for you:
<table>
{foreach from=$data.elements item=element name=elements}
{if $smarty.foreach.elements.iteration mod 6 eq 1}
<tr>
<td>
<table>
{/if}
{if $smarty.foreach.elements.iteration is odd}
<tr>
<td>
{/if}
<table align="{if $smarty.foreach.elements.iteration is odd}left{else}right{/if}">
Some content
</table>
{if $smarty.foreach.elements.iteration is odd}
<table align="left" cellspacing="0" cellpadding="0">
<tr>
<td width="100%" height="40" style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;"> </td>
</tr>
</table>
{else}
</td>
</tr>
{/if}
{if $smarty.foreach.elements.iteration mod 6 eq 0 or $smarty.foreach.elements.last}
</table>
</td>
</tr>
{/if}
{/foreach}
</table>
but because of many tables I cannot check it for sure. You could also look at Break UL tag after 'n' times in while-loop where I answered how to do the same in PHP.

Using "if" "else" in smarty table?

Here's the code I'm using for my table:
<tbody>
{foreach key=num item=referral from=$referrals}
{if $referral.commission neq "$0.00 USD"}
<tr>
<td>{$referral.date}</td>
<td>{$referral.service}</td>
<td>{$referral.amountdesc}</td>
<td>{$referral.commission}</td>
<td>{$referral.status}</td>
</tr>
{/if}
{foreachelse}
<tr>
<td colspan="6">{$LANG.norecordsfound}</td>
</tr>
{/foreach}
</tbody>
Basically if $referral.commission is $0.00 USD then I don't want that row to show. However, right now those rows are showing and I'm not sure what I'm doing wrong...
It's your quotes. Use single quotes and change the if statement to '$0.00 USD' instead of using double quotes.
<tbody>
{foreach key=num item=referral from=$referrals}
{if $referral.commission != '$0.00 USD'}
<tr>
<td>{$referral.date}</td>
<td>{$referral.service}</td>
<td>{$referral.amountdesc}</td>
<td>{$referral.commission}</td>
<td>{$referral.status}</td>
</tr>
{/if}
{foreachelse}
<tr>
<td colspan="6">{$LANG.norecordsfound}</td>
</tr>
{/foreach}
</tbody>
try this:
<tbody>
{foreach key=num item=referral from=$referrals}
{if $referral.commission neq "$0.00 USD"}
<tr>
<td>{$referral.date}</td>
<td>{$referral.service}</td>
<td>{$referral.amountdesc}</td>
<td>{$referral.commission}</td>
<td>{$referral.status}</td>
</tr>
{else}
<tr>
<td colspan="6">{$LANG.norecordsfound}</td>
</tr>
{/if}
{/foreach}
</tbody>
For more details to use if/else inside foreach loop refer http://www.smarty.net/docs/en/language.function.foreach.tpl

Smarty table even odd

I'm is using PHP smarty templater. I need to create even odd row highlighting.
Please, send me example how to do that.
also I have variable:
$smarty.foreach.product.index
For this kind of situation smarty has method called {cycle}
<table>
{foreach $products as $product}
<tr class="{cycle values="odd,even"}">
<td>{$product.name}</td>
</tr>
{/foreach}
</table>
The result for this will be:
<table>
<tr class="odd">
<td>1st product</td>
</tr>
<tr class="even">
<td>2nd product</td>
</tr>
<tr class="odd">
<td>3rd product</td>
</tr>
</table>
In you stylesheet file define properties for odd and even lines like this:
tr.even td{background: #CCCCCC;}
tr.odd td{background: #EFEFEF;}
<table>
{foreach key=i item=row from=$items}
<tr{if $i%2==1} bgcolor=#e4e4e4{/if}><td>{$i}</td></tr>
{/foreach}
</table>
{section name=myloop start=0 loop=10 step=1}
<tr class="{if $smarty.section.myloop.index is even}tr_even{else}tr_odd{/if}"><td>{$smarty.section.myloop.index}</td></tr>
{/section}

Passing a smarty foreach cycle variable into php code

I have foreach loop in smarty:
{foreach from=$clients item=client}
<tr class="{cycle values="erow,"} elements">
<td class="tdcenter no-label">{$client->id}</td>
<td>{$client->name}</td>
<td>{$client->email}</td>
<td>{$client->phone}</td>
<td>{php} echo get_client_profit($client->name);{/php}</td>
</tr>
{/foreach}
I need to pass smarty variable {$client->name} to php function get_client_profit. How should I do that? Is it possible?
I did this on my development machine and it works:
{foreach from=$clients item=client}
<tr class="{cycle values="erow,"} elements">
<td class="tdcenter no-label">{$client->id}</td>
<td>{$client->name}</td>
<td>{$client->email}</td>
<td>{$client->phone}</td>
<td>{get_client_profit($client->name)}</td>
</tr>
{/foreach}
Simply call the function without the {php} tags
Try this :
{foreach from=$clients item=client}
<tr class="{cycle values="erow,"} elements">
<td class="tdcenter no-label">{$client->id}</td>
<td>{$client->name}</td>
<td>{$client->email}</td>
<td>{$client->phone}</td>
<td>
{assign var="clientname" value=$client->name }
{php} echo get_client_profit($smarty->get_template_vars('clientname'));{/php}</td>
</tr>
{/foreach}

Categories