Jump to content

how to call php function in .tpl page


Recommended Posts

I have a public function in php page like this



public function _getProductID($id_product)
	{
		return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `id_product` FROM `'._DB_PREFIX_.'packing_product` WHERE `id_product` = '.(int)$id_product);
	}

public function hookDisplayProductAdditionalInfo($params)
    {
		$id_shop 	= Context::getContext()->shop->id;
		$products = $this->_getProducts($id_shop);
		$this->context->smarty->assign([
				'products' =>$products,
				'page' =>$this,
		]);
		return $this->display(__FILE__, 'product.tpl');
	}

 

I also have a .tpl file (product.tpl) with a script like this

{foreach from=$products item=p}
     {assign var='id_Product' value=$page->_getProductID($p.id_product)}
{/foreach}

but the result is an error with notes like this:

Fatal error: Uncaught Error: Cannot use object of type Extlink as array in

however in the back office page it works fine..

Which script code do I need to fix?

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

11 minutes ago, ventura said:

you have to do it in a module. The hookDisplayProductAdditionalInfo function has to be included in the main class of the module. 
You can create an example structure for the module here 
https://validator.prestashop.com/generator

Yes, I'm making a module. I see the problem in this line (in php page)

$this->context->smarty->assign([			
			'page' =>$this->,
		]);

'page' =>$this->,

if I remove this line, the module works fine. however I need a php function call on the .tpl page

Link to comment
Share on other sites

1 hour ago, ventura said:

you need a frontend controller in the module

any example??

 

I want to call this public function

public function _getProductID($id_product)
{
	return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('SELECT `id_product` FROM `'._DB_PREFIX_.'packing_product` WHERE `id_product` = '.(int)$id_product);
}

file : modules/mymodule/mymodule.php

and this is my hook function

public function hookDisplayProductAdditionalInfo($params)
{
	$id_shop 	= Context::getContext()->shop->id;
	$products = $this->_getProducts($id_shop);
	$this->context->smarty->assign([
		'products' =>$products,
		'page' =>$this,
	]);
	return $this->display(__FILE__, 'product.tpl');
}

file : modules/mymodule/mymodule.php

and this is tpl page

{foreach from=$products item=p}
     {assign var='id_Product' value=$page->_getProductID($p.id_product)}
{/foreach}

file: modules/mymodule/views/templates/hook/product.tpl

The error is caused by this script:

'page' =>$this,

file : modules/mymodule/mymodule.php

and this:

value=$page->_getProductID($p.id_product)

file: modules/mymodule/views/templates/hook/product.tpl

 

Link to comment
Share on other sites

Posted (edited)
5 hours ago, ventura said:

I didn't understand it well

 

try this

'page' => $this->_getProductID($params['product']->id)

 

However, I need to get the value after {foreach} on the .tpl page, like this example:

{foreach from=$countries item=c} 
	{assign var='id_country' value=$page->_getCountID($c.id_country)} 
{/foreach} 
{foreach from=$products item=p} 
	{assign var='id_product' value=$page->_getProductID($p.id_product)} 
{/foreach}

and, on the smarty array in .php file

$this->context->smarty->assign([
			'countries' =>$countries,
			'products' =>$products,
			'page' =>$this,
        ]);
return $this->display(__FILE__, 'product.tpl');

this script causes an error

'page' =>$this,

 

BAD ENGLISH, Sorry

 

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

better to make another type of approach
Modify the _getProducts function, either by modifying the query to add the _getProductID value, or directly modifying or creating a new array to add a new key. eg

 

public function hookDisplayProductAdditionalInfo($params)
{
    $id_shop = Context::getContext()->shop->id;
    $products = $this->_getProducts($id_shop);

    $product_with_ids = array();
    foreach ($products as $product) {
        $id_product_from_db = $this->_getProductID($product['id_product']);
        $product_with_ids[] = array_merge($product, ['id_product_from_db' => $id_product_from_db]);
    }
    $this->context->smarty->assign([
        'products' => $product_with_ids
    ]);

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

in tpl

{foreach from=$products item=product}
{$product.id_product_from_db}
{/foreach}

 

Link to comment
Share on other sites

4 hours ago, ventura said:

better to make another type of approach
Modify the _getProducts function, either by modifying the query to add the _getProductID value, or directly modifying or creating a new array to add a new key. eg

 

public function hookDisplayProductAdditionalInfo($params)
{
    $id_shop = Context::getContext()->shop->id;
    $products = $this->_getProducts($id_shop);

    $product_with_ids = array();
    foreach ($products as $product) {
        $id_product_from_db = $this->_getProductID($product['id_product']);
        $product_with_ids[] = array_merge($product, ['id_product_from_db' => $id_product_from_db]);
    }
    $this->context->smarty->assign([
        'products' => $product_with_ids
    ]);

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

in tpl

{foreach from=$products item=product}
{$product.id_product_from_db}
{/foreach}

 

Thank You.. I will try this..

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...