Jump to content

Calling value of a function of a module within a file theme tpl


freuxbang

Recommended Posts

Hi, I need to call in "shopping-cart-product-line.tpl" the value that returns a function located within a module.

 

What should I write in the file "shopping-cart-product-line.tpl"?

 

 

 

Name module: Pippo

 

/modules/Pippo/controllers/front/actions.php

 

 

public function pipposhow() {

       $pipposhow= 2;

       echo $pipposhow ;

    }
 

 

 

 

/themes/mytheme/shopping-cart-product-line.tpl

I want to draw the function result pipposhow
 

thank you

Link to comment
Share on other sites

Thank you for responding
Could you show me how to assign the module smarty?
 
What should I write inside the module Pippo?
 
/modules/Pippo/controllers/front/actions.php
public function pipposhow() {

$pipposhow= 2;
echo $pipposhow ;


    } 

/modules/Pippo/pippo.php

Here I have to assign the variable smarty?

/themes/mytheme/shopping-cart-product-line.tpl

I want to draw the function result pipposhow

you kindly tell me what to put in these files?

 

thank you

Link to comment
Share on other sites

You don't assign a module to smarty, you assign a variable.

 

In the pipposhow method:

public function pipposhow() 
{
$pipposhow= 2;
$this->context->smarty->assign('pipposhow', $pipposhow) ;
} 

Then you invoke the variable in the tpl:

 

{if isset($pipposhow)}{$pipposow|intval}{/if}

 

That said, you'd better, in this case, transfer the logic (ie the piposhow method) to the module itself (ie not a module controller) and make it a hook that you can invoke from the shopping-cart-product-line.tpl.

 

Modify your module's constructor in order to register a new hook (pippoShow, for instance). And put the hook method:

public function hookPippoShow() 
{
$pipposhow= 2;
return $pipposhow;
}

or

public function hookPippoShow() 
{
$pipposhow= 2;
$this->context->smarty->assign('pipposhow', $pipposhow) ;
return $this->display(__FILE__, 'pipposhow.tpl')
} 

which is probably better if you decide to have a more elaborate display in mind.

 

Reinitialise your module.

 

Then call the hook directly from the shopping-cart-product-line template:

 

{hook h='pippoShow' mod='Pippo'}

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

Thank You
 
I tried to enter the code as you suggested, but I can not get the tick value $pipposhow in shopping-cart-product-line.tpl
 
What I do is this
 
/modules/pippo/pippo.php
 
public function hookPipposhow() 
{
$pipposhow= 2;
$this->context->smarty->assign('pipposhow', $pipposhow) ;
return $this->display(__FILE__, 'pipposhow.tpl')
}
Then I go on shopping-cart-product-line.tpl and insert
 
{hook h='pipposhow' mod='pippo'}

But I do not check any value...

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

Check that the hook has been properly registered. Be sure to include the registerhook call in the module's install method (ie function). Remember that your value is to be used in the pipposhow.tpl that you have to place in your module's views/hook directory.

 

pipposhow.tpl could be like

{l s='Pippo show value:' mod='pippo'}
{if isset($pipposhow)}{$pipposhow|intval}{/if}

the output is then injected into shopping-cart-product-line

Link to comment
Share on other sites

Ok thanks, I was able to check the value, what I was missing was the part of the installation of the block.
 
I realized, however, that using this system can not be printed for each line of the cart a different value.
 
On shopping-cart-product-line I added:
 
{hook h='pipposhow' mod='pippo'}
 
I attach a picture to help you understand
 
On each line of product I always print the contents of pipposhow.tpl
 
How should I do to print a different value on each line of the product?

2cs9fdl.png

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

What is the value (or, more generally, the type of data) you want to output? On what is it based? If it's, for instance, based on the product, you can pass the product (or whatever variable you want) as parameter:

 

{hook h='pipposhow' mod='pippo' product=$product othervar=varvalue}

 

In your hook code, you can retrieve the product like so:

 

$product = $params['product']

 

 

don't forget, though, to add the $params parameter to the definition of the hook method:

 

public function hookPipposhow($params)

{

    $product = $params['product'];

    $othervar = $params['othervar'];

    your code

}

Link to comment
Share on other sites

Do not think I've got it right.
 
Let me give an example of what I want to achieve.
 
I have on my cart 2 products
 
ID Prodotto1 = 22
ID Prodotto2 = 262
 
This is what I put on pippo.php
 
public function hookPipposhow($params)
{
    $products = $this->context->cart->getProducts();
        foreach ($products as $key => $val ) {
             $id_product = $products[$key]['id_product'] ;
           
        
        
        $this->context->smarty->assign(
                array('id_product' => $id_product));
        
        }
        
        return $this->display(__FILE__, 'pipposhow.tpl');
        
 
 
}
 
 
I put this on pipposhow.tpl
 
{$id_product}
 
 
I put this on shopping-cart-product-line.tpl
 
{hook h=‘pipposhow’ mod=‘pippo’}
 
 
The result is this:
 
212wt1y.png
 
I would like famished this:
 
2m3lkxk.png
 
Obviously this is an example, actually I should check to other values.

Link to comment
Share on other sites

You don't have to get the cart products each time because you already have them (the hook is invoked from the shopping-cart-product-line and the cart's products have already been assigned to smarty). You just need a reference to the current product being displayed.

 

{hook h='pipposhow' mod='pippo' product=$product}

 

 

public function hookPipposhow($params)
{
        $product = $params['product'];
        $this->context->smarty->assign(array('id_product' => $product['id_product']));
        return $this->display(__FILE__, 'pipposhow.tpl');
       
}

  • Like 1
Link to comment
Share on other sites

  • 4 years later...
On 2/8/2015 at 8:23 PM, razaro said:

Have you register that hook in install method with


$this->registerHook('pipposhow')

and in back office Module > Positions do you see module "hooked" there ?

Hi there.

I tried those modification because I have the same kind of need, but I can't see the new Hook I created in my module...
I have a module and in the install function I added

!$this->registerHook('ecoute')

so below I added that code

public function hookEcoute()
    {
        $ecoute=2;
        
$this->context->smarty->assign('ecoute', $ecoute) ;
return $this->display(__FILE__, 'ecoute.tpl');
    }

and then create an ecoute.tpl file.

in the end I can't see the new hook, in  modules/positions, and I can't show $ecoute value in product-list.tpl where I call
        {hook h='ecoute' mod='homefeatured'}

...

any help is welcome!

Link to comment
Share on other sites

Hi @Pat_07

 

This code was for an older version of PrestaShop it may not work in the latest 1.7.

 

But check modules/positions and check to display all hooks.

And your module is called homefeatured ? If not try to use your module name in 

 {hook h='ecoute' mod='homefeatured'}

 

Link to comment
Share on other sites

On 2/8/2015 at 10:46 PM, zombie process said:

pipposhow.tpl could be like


{l s='Pippo show value:' mod='pippo'}
{if isset($pipposhow)}{$pipposhow|intval}{/if}

I have the same kind of problem but with an array.

so what should $pipposhow|intval  become ?

$pipposhow|array ?

 

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