I'm doing this:
{if $categories->id !=86 && $categories->id !=87}
xxxx code
{/if}
The categories is a vector and has id's from 1 to 140 or so, the id's 86 and 87 exist, I don't want to do the code on the pages with id's 86 and 87, but it is not working properly.
This will always execute because when you are in category 86 it evaluates to:
if (true && false) // returns false
You need to use XOR operator
{if $categories->id !=86 xor $categories->id !=87}
xxxx code
{/if}
EDIT
Sorry missed the dont wan't to execute part
{if !($categories->id ==86 || $categories->id ==87)}
xxx code
{/if}
Should be fine
Related
Im trying to pass my own variable from module to template file .tpl
I have this code that is used for displaying availability in eshop. Product.tpl
{if $product.availability == 'available'}
{if $product.quantity <= 0 && $product.allow_oosp}
{if isset($product.available_date) && $product.available_date != '0000-00-00'}
<i class="fa fa-truck rtl-no-flip" aria-hidden="true"></i>
{$product.availability_message}
({if $product.available_date|strtotime > $smarty.now}<span class="available-date">{l s='naskladnění' d='Shop.Theme.Catalog'} {$product.available_date|date_format:"%d.%m.%Y"}</span>{/if})
{/if}
{/if}{/if}
Then Im having my own module where im assigning value to smarty
Mymodule.php
$in_stock = 1;
$this->context->smarty->assign("is_in_stock", $in_stock);
My question is if there is any way to access my smarty variable directly from theme tpl? I need to set up another {if else} with that variable but cant access it.
When I add variable to custom hook I cant access it neither.. Or maybe I dont know how. I tried to create front controller but nothing happened.
Something like
{if isset($product.available_date) && $mymodule.is_in_stock = 1 && $product.available_date != '0000-00-00'}
You can access functions of your module anywhere in .tpl files by
{YourModule::yourFunction()}
and set if like
{if YourModule::yourFunction() != 1}
Hello
{/if}
I use this code in Prestashop
{if (strpos($product.name, 'TVNUMBER1') !== false)}
THIS PRODUCT IS IN SALE
{/if}
So whenever I want to display that certain products are in sale, I have to go line by line, specifying the same product i.e."TVNUMBER1". I want to be able to write an array detailing all the products I have in sale "TV1, TV2, TV3", and get a code like this:
{if (strpos($product.name, '$array') !== false)}
THIS PRODUCT IS IN SALE
{/if}
I've tried similar examples found here, but I can't get them to work, either in Prestashop or in PHP testers online. It looks super simple, but I can't get around it.
I think what you want is the in_array php function, that check if a given $needle is or not in an array.
So what you should do is :
{if (in_array($product.name, '$array') !== false)}
THIS PRODUCT IS IN SALE
{/if}
Then in your controller you can assign the array to smarty :
$arr = array('TVNUMBER1', 'TVNUMBER2', 'TVNUMBER3');
$smarty->assign('myArray', $arr);
It seems you are using Smarty as template engine. So you could do something like this (from the doc).
In the controller
//Give it to the view
$arr = array('TVNUMBER1', 'TVNUMBER2');
$smarty->assign('myArray', $arr);
And in the view
//In the view, loop over the array
{foreach from=$myArray item=productName}
//If your product is among the in-sale ones, show the message
{if (strpos($product.name, productName) !== false)}
THIS PRODUCT IS IN SALE
{/if}
{/foreach}
how can I add/print tracking number on invoice?..
I used the code below
{l s='Tracking Number:' pdf='true'}
{$order->shipping_number}
returns blank, but on view order it has a tracking number. (image below)
orders->shipping_number field is blank.
Thanks.
adding on invoice.tpl
{foreach from=$order->getShipping() item=line}
{if $line.url != '' || $line.tracking_number != ''}
{'Tracking Number : '}
{$line.tracking_number}
{/if}
{/foreach}
solves the issue.
Is this even on the same planet as the correct code format in smarty.
{foreach from=$watchvendor item=item}
<td>{$item.id} - {$item.nick}</td>
{math equation= 'x/y' x=$item y='4' assign='howmany'}
{if $howmany eq 0}
</tr>
<tr>
{/if}
{/foreach}
i assume that $item is the element identity in the array ie.. 0 1 2 3 4 5 so when that did not work i tried this as well
{math equation=" 'x/y' x=count($watchvendor) y='4' assign='howmany'}
so basically if the loop divided by 4 = 0 its time for a new row.
The basic code without the extra math works fine i just want it spread out on the page.
ok i used
{$item|#debug_print_var}
and got this
Array (2)
id => 8
nick => "bbuddy" Array (2)
id => 7
nick => "span" Array (2)
id => 6
nick => "LJ" Array (2)
id => 5
nick => "JD" Array (2)
id => 4
nick => "Jsmith159"
sure is a strange looking array, first time i have ever seen a smarty array - no element id's
This seems to work...
<table class="ow_table_1 ow_form ow_automargin">
<tr>
{assign var=cnt value=0}
{foreach from=$watchvendor item=item}
{assign var=cnt value=$cnt+1}
<td>{$item.id} - {$item.nick}</td>
{if $cnt eq 4}
</tr>
<tr>
{assign var=cnt value=0}
{/if}
{/foreach}
</tr>
</table>
I am using Smarty to output an array to an HTML table. I want each row of the table to have no more than 8 items in it. If the array has more than 8 items then the code would make a new row for the overflowing items.
How can I do this? Is this clear?
It's been a long time since I've used Smarty, but you should be able to do this like this:
<tr>
{foreach from=$items key=myId item=i name=foo}
{if $smarty.foreach.foo.index % 8 == 0 && $smarty.foreach.foo.index > 0 }
</tr><tr>
{/if}
<td>{$i.label}</td>
{/foreach}
</tr>
The modulus operator only returns 0 if the index is dividable by 8, So before every 9th item it adds a new row. We don't want this for the first item to happend so let's check that as well.
Here's how I did it in the past:
<table>
{foreach from=$array item='array_item' name='array_items'}
{if $smarty.foreach.array_items.first}
{* first item - start of all the rows *}
<tr><td>{$array_item}</td>
{elseif $smarty.foreach.array_items.index % 8 == 0}
{* 8 items added to row - start new row *}
</tr><tr><td>{$array_item}</td>
{elseif $smarty.foreach.array_items.last}
{* last item - end the row (or add logic to fill out row with empty cells if needed) *}
<td>{$array_item}</td></tr>
{else}
{* normal item - add cell *}
<td>{$array_item}</td>
{/if}
{/foreach}
</table>