Jump to content

Module, hook, function ...?


Recommended Posts

Hi there.

I am a bit lost in the Prestashop 1.6 website I have to modify.

I have a old module (done by someone else..) that calculates and displays a specific result for a few products on home page.
I would like to use that module but to get the result in product-list.
Not clear isn't it...

For each product displayed in product-list, I woudl like to pass the product id to a function (or hook...) in that module, which make the caluclation and send me back the result (no need to use smarty variable, isn't it ?).
Don't know if I have to use a new hook in my module, but it is no matter of display or action, just a calculation.

I read a lot of tutos but can't find help.

Thanks for your time.

Link to comment
Share on other sites

Thank you @joseantgv.

The fucntion is 2 lines code

$hashfilename = ProductDownload::getFilenameFromIdProduct($params['id_product']);
			$productsMP3Info[$params['id_product']] = md5("aqf15ergh15g1serg3q5eg1qe56rg1".ProductDownload::getFilenameFromFilename($hashfilename)."cmszdf289r1h152fr481z15g12g");
			

So I want to use that code from product-list for each product displayed.

Link to comment
Share on other sites

I created a hook function in my module

function hookLibreecoute($params)
	{
			$hashfilename = ProductDownload::getFilenameFromIdProduct($params['id_product']);
			$productsMP3Info[$params['id_product']] = md5("aqf15ergh15g1serg3q5eg1qe56rg1".ProductDownload::getFilenameFromFilename($hashfilename)."cmszdf289r1h152fr481z15g12g");
			$smarty->assign(array('productsMP3Info' => $productsMP3Info));
	

	}

Do I have to set a line of code with return instruction ?

And how can I call it in my tpl product -list file?
With product as params for the function...

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

If you create a new hook, you have to insert it to template file.

What @joseantgv was saying is i guess: Open up product-list.tpl file, look for hooks there. For example displayProductListFunctionalButtons.

If none of these hooks are in the position you are after, you have to insert your hook ( libreecoute ) where you need to display custom information.

You can also see how you can pass product as parameter

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

hace 44 minutos, Pat_07 dijo:

I created a hook function in my module


function hookLibreecoute($params)
	{
			$hashfilename = ProductDownload::getFilenameFromIdProduct($params['id_product']);
			$productsMP3Info[$params['id_product']] = md5("aqf15ergh15g1serg3q5eg1qe56rg1".ProductDownload::getFilenameFromFilename($hashfilename)."cmszdf289r1h152fr481z15g12g");
			$smarty->assign(array('productsMP3Info' => $productsMP3Info));
	

	}

Do I have to set a line of code with return instruction ?

And how can I call it in my tpl product -list file?
With product as params for the function...

The function should return a value, not assign to smarty object.

In this case you need to add the hook in the template:

{hook h='libreecoute' product=$product}

and get the product in the PHP function with:

$params['product']

You also need to create this hook in database and hook your module in the position.

  • Like 1
Link to comment
Share on other sites

Thanks @Kert L and @joseantgv

So I called the hook from product-list with {hook h='libreecoute' product=$product}
Howerer I didn't unerstand how to use the parameter in the new hook in php file.

what I need in my new hook is the Id of a product.

so my call should not be somethign like {hook h='libreecoute' myvariable=$product[id-product]} ?

And inside the php file I don't know how to use the parameter ($params ?) or do I need to use a smarty variable to pass it?

function hookLibreecoute($params)
	{
$hashfilename = ProductDownload::getFilenameFromIdProduct(myvariable);
etc...
}

 

Link to comment
Share on other sites

32 minutes ago, Pat_07 said:

Thanks @Kert L and @joseantgv

So I called the hook from product-list with {hook h='libreecoute' product=$product}
Howerer I didn't unerstand how to use the parameter in the new hook in php file.

what I need in my new hook is the Id of a product.

so my call should not be somethign like {hook h='libreecoute' myvariable=$product[id-product]} ?

And inside the php file I don't know how to use the parameter ($params ?) or do I need to use a smarty variable to pass it?


function hookLibreecoute($params)
	{
$hashfilename = ProductDownload::getFilenameFromIdProduct(myvariable);
etc...
}

 

$params is an array of parameters. If execute hook in template then "myvariable" will be accessable as $params['myvariable'].

It's okay to pass just $product and then access its id as $params['myvariable']['id_product']
( if $product is array )

  • Like 2
Link to comment
Share on other sites

Thank you @Kert L!

In my function hook I should not set a return line?

or do I use a smarty variable to get the result?

function hookLibreecoute($params)
	{
			$hashfilename = ProductDownload::getFilenameFromIdProduct($params['product']['id_product']);
			$productsMP3Info[$params['product']['id_product']] = md5("aqf15ergh15g1serg3q5eg1qe56rg1".ProductDownload::getFilenameFromFilename($hashfilename)."cmszdf289r1h152fr481z15g12g");
			
			$smarty->assign(array('productsMP3Info' => $productsMP3Info));
	}

I don't understand if I need to use a smarty variable or can I just use something like that

return md5("aqf15ergh15g1serg3q5eg1qe56rg1".ProductDownload::getFilenameFromFilename($hashfilename)."cmszdf289r1h152fr481z15g12g");

Then in my product-list.tpl I just call the hook that way ?
        

{hook h='libreecoute' mod='mymoule' product=$product}


but I don't understand how to use the result in tpl file.

Link to comment
Share on other sites

If you just want to display the hook result, in your case the "productsMP3Info" then yes just return in your hook function

 

function hookLibreecoute($params)
	{
			$hashfilename = ProductDownload::getFilenameFromIdProduct($params['product']['id_product']);
			$productsMP3Info[$params['product']['id_product']] = md5("aqf15ergh15g1serg3q5eg1qe56rg1".ProductDownload::getFilenameFromFilename($hashfilename)."cmszdf289r1h152fr481z15g12g");
			
			//$smarty->assign(array('productsMP3Info' => $productsMP3Info));

			return $productsMP3Info;
	}

In template place 

{hook h='libreecoute' mod='mymoule' product=$product}

After that, where ever your hook is, its output will be the "$productsMP3Info"

This should do for your case.

  • Like 1
Link to comment
Share on other sites

Sorry I solved the error !!!
I get back a array rather than just one element.

However I have a syntax problem in tpl.
The hook return me the name of the file but I don't know how to add it to a code part

{include file="mp3player.tpl"
mp3file="$mp3filename?p=$productID&key=the_return_value_from_my_hook"
mp3width='175' mp3height='20'}

how can I replace the_return_value_from_my_hook with {hook h='libreecoute' mod='mymoule' product=$product} ???

Link to comment
Share on other sites

Do you have custom template mp3player.tpl?

I think the best way to solve this is to return html from the hook

function hookLibreecoute($params)
{
	$this->context->smarty->assign(YOUR_VARIABLES);
	
	return $this->context->smarty->fetch(YOUR_TEMPLATE);
}

YOUR_VARIABLES = Whatever variables you use in your template

YOUR_TEMPLATE = mp3player.tpl

Then in your product template when you call the hook, you already return the HTML from mp3player.tpl
 

 

  • Like 1
Link to comment
Share on other sites

Hi there.

Just to tell I didn't know how to use fetch so I found another way.
Actually I created the whole url with all parameters in my hook and I return it.

It wa seasier and it worked.

Thanks all for your help.

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