Jump to content

Loop trough products information and display it in a .tpl file


LazyCircles

Recommended Posts

Hello Prestashop Community,

 

I'm creating a custom module to show product information inside a custom map.

I think the solution is to loop trough an array of products and get their values from declared variables.

 

Let's say we have this product in the BO.

 

Product name: "Deck of cards"

Price: 20$

Description: "Lorem Ipsum..."

Combination: "Red, yellow, green" 

 

I want to be able to get this values and show them inside tpl file. So I can do something like this.

 

<div>
<h3>Product name: $product_name</h3>

<p>Price: $product_price</p>
</div>

 

Is this possible? Am I thinking right? Is there any other solution?

I've look through forums but i can't find any solution, maybe Im working inside the wrong files cuz I'm always getting undifined variables...

 

All help is appreciated.

Thanks!

 

Best Regards,

Bruno 

 

 

 

 

Link to comment
Share on other sites

Isn't that exactly what is done in the product-list.tpl?

 

Hello musicmaster! Thanks for your fast response. It is but I want to be able to do this inside a custom module but somehow i can't get the values of the products. I always get undifined variables :(

 

Best regards,

Bruno

Link to comment
Share on other sites

 

Hi, you can achieve it this way:

<?php

//in your module

public function getProductforMap(){

//let's get the product id at first:
if(Tools::getValue('id_product')!='')
{$id_product=Tools::getValue('id_product');}
else 
{$id_product=Context::getContext()->cookie->id_product;}

//let's now create the corresponding product:

$product=new Product((int)$id_product);

$name=$product->name;
$description=$product->description;
$price=$product->price;

//etc.  And send them to your template

$this->context->smarty->assign('name' ,$name);	
$this->context->smarty->assign('description' ,$description);	
$this->context->smarty->assign('price' ,$price);	

 return $this->display(__FILE__, 'your_template_name.tpl');	


}

ndiaga,

I really appreciate your help! Going to try it, I'll give you feedback then :)

 

Best regards,

Bruno

Link to comment
Share on other sites

Hello, I applied this to my code, ehrrm.. I still get those undifined variables:

 

Notice: Undefined index: name in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 41

Notice: Trying to get property of non-object in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 41

Notice: Undefined index: description in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 43

Notice: Trying to get property of non-object in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 43

Notice: Undefined index: price in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 45

Notice: Trying to get property of non-object in /tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 45

 

I used print_r on the variables and they are returning "1", what does this mean?

Thanks!

 

Best regards,

Bruno

 

 

Edited by LazyCircles (see edit history)
Link to comment
Share on other sites

The template in witch you are calling these variables should be controlled by the function in witch the variables are definded.

What is the name of your template and did you call it? 

Hello thanks for response,

 

Yes I called it like this: return $this->display(__FILE__, 'quartoscoimbra.tpl'); located in the /views/front/quartoscoimbra.tpl

Link to comment
Share on other sites

 

This way?

public function getProductforMap(){

//let's get the product id at first:
if(Tools::getValue('id_product')!='')
{$id_product=Tools::getValue('id_product');}
else 
{$id_product=Context::getContext()->cookie->id_product;}

//let's now create the corresponding product:

$product=new Product((int)$id_product);

$name=$product->name;
$description=$product->description;
$price=$product->price;

//etc.  And send them to your template

$this->context->smarty->assign('name' ,$name);	
$this->context->smarty->assign('description' ,$description);	
$this->context->smarty->assign('price' ,$price);	

 return $this->display(__FILE__, 'quartoscoimbra.tpl');	


}

Yes! Do you think it's some file error that's provoking it? 

I already did var_dump and used {debug} to see if there's any value inside the variables, but it show me NULL :(

Link to comment
Share on other sites

 

Are you using this function in a module?

And in the quartoscoimbra.tpl  file you should call those variables this way:

{$name}<br/>
{$description}<br/>
{$price}

Yes. I am, the module is called QuartosCoimbra. Im using the <php> code that you gave me in quartoscoimbra.php that is located inside the root folder of the module.

I'm calling those variables in tpl file exactly as you say. 

Already passed a value, using a variable I created for test and it pass the value, so I think the problem comes from the php file. Do I need to do something before using that public function? like a sql query or a loop to get all products information? (I think its already done in the code you shared).

Sorry I'm a prestashop newbie I have some dificulties working around this smarty thing... :)

 

Many Thanks for your patience!

Edited by LazyCircles (see edit history)
Link to comment
Share on other sites

No, you don't need to any sql query. This code should work.

But I see you want to loop from all the products in your shop right?

Or you just need to get information from a single product.

I want to have a loop :) I want to get the all the categories that are in my store and loop all products of those categories.

Something like a "categorie == store in the map" and that store has many products assigned to that category.

 

Thanks!

Link to comment
Share on other sites

 

Ok, so you should use this function instead:

public function getProductAllforMap(){

$productObj = new Product();
$id_lang=Configuration::get('PS_LANG_DEFAULT');
$products = $productObj -> getProducts($id_lang, 0, 0, 'id_product', 'DESC' );

$this->context->smarty->assign('all_product' ,$products);	

 return $this->display(__FILE__, 'quartoscoimbra.tpl');	

}

And here is your .tpl  file:

{foreach from=$all_product item=product}

{$product.name}<br/>
{$product.price}</br>
{$product.description}

{/foreach}

Many many thanks! I'm not home, I'm going to try it asap. I will give you feedback then :)

Link to comment
Share on other sites

 

Ok, so you should use this function instead:

public function getProductAllforMap(){

$productObj = new Product();
$id_lang=Configuration::get('PS_LANG_DEFAULT');
$products = $productObj -> getProducts($id_lang, 0, 0, 'id_product', 'DESC' );

$this->context->smarty->assign('all_product' ,$products);	

 return $this->display(__FILE__, 'quartoscoimbra.tpl');	

}

And here is your .tpl  file:

{foreach from=$all_product item=product}

{$product.name}<br/>
{$product.price}</br>
{$product.description}

{/foreach}

 

Man, YOU ARE THE BEST! thank you so much, its working now :D yeii.

Many thanks for your help and patience around this topic.

 

Best Regards,

Bruno

Link to comment
Share on other sites

I'm glad you solve your problem. We are here to share.

Thank you for the support ndiaga, Can you help with one more problem? If I want to make the same loop but instead of products, I want categories? What am I doing wrong?

Smarty php

$categoryObj = new Category();
    $id_lang=Configuration::get('PS_LANG_DEFAULT');
    $categories = $categoryObj -> getCategories($id_lang, 0, 0, 'id_category', 'DESC' );  
        
    $this->context->smarty->assign('all_category' ,$categories);

And in .tpl

{foreach from=$all_categories item=category}
   {$category.name}
{/foreach}

Thanks!

 

Best regards,

Bruno

Link to comment
Share on other sites

Hi Bruno,

For categories, the function is a bit different.

In your module create this function:

  public static function getCategories($id_lang = false, $active = true, $order = true, $sql_filter = '', $sql_sort = '', $sql_limit = '')
	{
	 	if (!Validate::isBool($active))
	 		die(Tools::displayError());
		$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
			SELECT *
			FROM `'._DB_PREFIX_.'category` c
			'.Shop::addSqlAssociation('category', 'c').'
			LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').'
			WHERE 1 '.$sql_filter.' '.($id_lang ? 'AND `id_lang` = '.(int)$id_lang : '').'
			'.($active ? 'AND `active` = 1' : '').'
			'.(!$id_lang ? 'GROUP BY c.id_category' : '').'
			'.($sql_sort != '' ? $sql_sort : 'ORDER BY c.`level_depth` ASC, category_shop.`position` ASC').'
			'.($sql_limit != '' ? $sql_limit : '')
		);

		if (!$order)
			return $result;

		$categories = array();
		foreach ($result as $row)
			$categories[$row['id_category']]['infos'] = $row;

		return $categories;
	}	
// and call it this way
And call it this way:

$id_lang=Configuration::get('PS_LANG_DEFAULT');
$categories = YourModuleClassName::getCategories($id_lang, true, false) ;

// and send $categories to smarty
And send $categories to smarty as you did.

Hello ndiaga,

 

Sorry for the late response.

Already tested your function, I created the public static function in mymodule.php the sql is ok I get all information from category array (tested with print_r) but when I pass it to tpl gives me undifined variable. I tried to assign variable as we did with products smarty->assign, but nothing.

 

I dont know if its related but Im using displayHome hook to send results to tpl page (i did it with products, and it works) and the custom "public static function" doesn't "show" results in homepage. (Tried print_r inside and nothing changes). Do I need to hook it? Or assign to tpl file? (Tested both but nothing changed).

 

After get this working I need to have a function to get all categories and then show all products from the same category. (Combining both functions you made) Is it possible to make right? I think category.tpl have it but I always feel a bit lost in the code :(

 

Thanks in advance!

 

Best regards,

Bruno

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...