How to loop over an array in Smarty/Prestashop? - php

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}

Related

prestashop url to category

[PRESTASHOP 1.7] I want to display categories outside of ps_categorytree.
Here is the module code:
$allCategories = Category::getNestedCategories(null, $this->context->language->id);
$this->context->smarty->assign( 'allCategories' , $allCategories );
return $this->fetch('module:'.$this->name.'/views/templates/widget/block.tpl');
.tpl file:
{foreach from=$allCategories item=mainCategory}
{$mainCategory.name}
{foreach from=$mainCategory.children item=subCategory}
{$subCategory.name}
{/foreach}
Error: Notice: Undefined index: link
How do I assign a link?
Because of you got the Category as array, not an object.
Try to use mainCategory['link'] instead of mainCategory.link, and so on
UPDATE
I misunderstood the question, sorry!
Try this.
{foreach $allCategories as $mainCategory}
{$link->getCategoryLink($mainCategory.id_category, $mainCategory.link_rewrite)|escape:'html':'UTF-8'}
{/foreach}
If you have'nt $link variable in the smarty file, you should assign it
'link' => $this->context->link,

Prestashop custom module variable passed to Theme .tpl file

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}

Print tracking number on prestashop invoice

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.

Codeigniter categories and sub categories function and pass to smarty variable

This is my codeigniter code for categories and sub categories when i echo the results outside the first foreach or pass results to smarty it only outputs last rows with last main id.
But echo within the first foreach before closing } returns all results.
function getAllCats(){
$this->load->model('mHtml', 'mnMod');
$main_cat = $this->mnMod->getncats();
$all_cat = '';
foreach($main_cat as $mcat){
$all_cat = '<li><h3>'.$mcat->cname.'</h3>';
$sub_cat = $this->mnMod->getscats($mcat->categoryid);
foreach($sub_cat as $scat){
$all_cat .= ''.$scat->cname.'<br />';
}
$all_cat .= '</li>';
}
// echo $all_cat; die;
$this->smarty->assign("nav", $all_cat);
}
Results returned with mymethod.
Power Inverters
Off Grid Pure Sine Wave
Grid Tie String
Micro Grid Tie
Results i want to achieve and pass to smarty variable
Solar Panels
Monocrystalline
Polycrystalline
Flexible Solar
Charge Controllers
PWM
MPPT Technology
Power Inverters
Off Grid Pure Sine Wave
Grid Tie String
Micro Grid Tie
I have the same issue what i did i get all parent categories by one mysql query and in another i select all categories, simply i assign variables to smarty and foreach those results against each parent category.
$main_Categories = $this->model->all_categories();
$this->smarty->assign("main_Categories ", $main_Categories );
//On Smarty page
{foreach $main_Categories $cat}
{if $cat->parentid = 'your parent id'} // parent id will be same for all main ids.
{$cat->category_name}<br />
{assign 'cat_id' $cat->cat_id}
{foreach $main_Categories as $sub}
{if $cat_id eq $sub->parent_id}
{$sub->category_name}<br />
{/if}
{/foreach}
{/if}
{/foreach}
Loop through your $main_cats and assign sub_categories for each one. Good practice is to ensure $main_cats is an array before doing this.
Controller:
function getAllCats() {
$this->load->model('mHtml', 'mnMod');
$main_cats = $this->mnMod->getncats();
if(is_array($main_cats))
foreach($main_cats as &$mcat) {
$mcat['subcat'] = $this->mnMod->getscats($mcat->categoryid);
}
$this->smarty->assign("cats", $main_cats);
}
View:
{if $cats}
{foreach from=$cats item=cat}
<li><h3>{$cat.cname}</h3>
{foreach from=$cat item=sub_cat}
{$sub_cat.cname}<br />
{/foreach}
</li>
{/foreach}
{else}
<p>No cats found</p>
{/if}

Smarty assign and display two dimensional array

I want to get a two dimensional array's data and display it in a html file using smarty:
The idea is as following: my array contains several arrays everyone contains the category name in the first offset and the attached links to this category
1-file php
$categories_links = array();//array that contains some catgories name with the attached links
//some dummy data
$categorie1="Horror movies";
$link11="http://www.movie11.com";
$link12="http://www.movie12.com";
$link13="http://www.movie13.com";
$categories_links[] = array($categorie1, $link11, $link12,$link13);
$categorie2="Action movies";
$link21="http://www.movie21.com";
$link22="http://www.movie22.com";
$categories_links[] = array($categorie2, $link21, $link22);
$smarty->assign('categories_links' , $categories_links );
$smarty->display('file.html');
2-file html
{foreach key=categorie item=categorie from=$categories_links}
foreach key=categorie item=categorie from=categorie}
<!--
1.display only the first item in every array as the category name
2.display the rest as the links attached to the above category
//-->
{/foreach}
{/foreach}
Assuming you use Smarty 3 (you haven't mentioned anything about Smarty 2) you can use the following code:
{foreach $categories_links as $categorie}
<p>
{foreach $categorie as $item}
{if $item#first}
<strong>Category name: {$item}</strong><br />
{else}
{$item}
{/if}
{/foreach}
</p>
{/foreach}
Output for this will be:
Category name: Horror movies
http://www.movie11.com http://www.movie12.com http://www.movie13.com
Category name: Action movies
http://www.movie21.com http://www.movie22.com
EDIT
As you mentioned in comment you want solution for Smarty 2 you need to use in you Smarty template file:
{foreach key=id item=categorie from=$categories_links}
<p>
{foreach item=item from=$categorie name=list}
{if $smarty.foreach.list.first}
<strong>Category name: {$item}</strong><br />
{else}
{$item}
{/if}
{/foreach}
</p>
{/foreach}
This will give you output:
Category name: Horror movies
http://www.movie11.com http://www.movie12.com http://www.movie13.com
Category name: Action movies
http://www.movie21.com http://www.movie22.com
(exactly the same as the one in Smarty 3)
I'd refactor the data array to use the category name as a key.
$categories = array(
'Horror movies' => array(
'link1',
'link2',
/...
),
'Action movies' => array(
'link1',
'link2',
/...
),
);
$smarty->assign("categories", $categories);
Then you can use it easily in Smarty
{foreach from=$categories key=category item=links}
Category: {$category}
{foreach from=$links item=link}
{$link}
{/foreach}
{/foreach}
It is much easier to use that way.

Categories