Jump to content

[solved] how to call the function and display the result from tpl file in prestashop


Recommended Posts

I need to display product favorite count and some other functionalites in product detail page (product.tpl page).

But i am new to prestashop.so i am unable to find the where i declared function and i dont how to call the function form tpl file.

Here i write product favorite count code

 

 

 

public function favcount($id_product)
{

	 $sql = 'SELECT count(*) FROM `ps_favorite_product` WHERE  `id_product`='.(int)$id_product.;


	$result = Db::getInstance()->getRow($sql);
	return  $result['count'];
}

 

 

in which place i can insert the above code and how to call from product.tpl file

any one help ?

  • Like 1
Link to comment
Share on other sites

well, you should edit product controller (add variable to the smarty array) then you will be able to display it (variable) in the .tpl file

 

please give me information about your prestashop version, i will show you how to achieve this.

  • Like 1
Link to comment
Share on other sites

thanks for informations. Okay, so open the classes/Product.php file

 

add your function to the controller, but change it a bit, here is a correct code:

public static function favcount($id_product){

	$sql = 'SELECT count(*) AS count FROM `ps_favorite_product` WHERE  `id_product`='.(int)$id_product;
	$result = Db::getInstance()->getRow($sql);
	return  $result['count'];
}

 

 

then in .tpl file (product.tpl located in your theme) you can use:

 

{Product::favcount(Tools::getvalue('id_product'))}

  • Like 8
Link to comment
Share on other sites

thanks for informations. Okay, so open the classes/Product.php file

 

add your function to the controller, but change it a bit, here is a correct code:

public static function favcount($id_product){

	$sql = 'SELECT count(*) AS count FROM `ps_favorite_product` WHERE  `id_product`='.(int)$id_product;
	$result = Db::getInstance()->getRow($sql);
	return  $result['count'];
}

 

 

then in .tpl file (product.tpl located in your theme) you can use:

 

{Product::favcount(Tools::getvalue('id_product'))}

 

 

thank you very much. I got .what i am exactly need.

Link to comment
Share on other sites

×
×
  • Create New...