PipBoy2000
PipBoy2000

Reputation: 440

How to get $product variables in custom module template in Prestashop 1.7

Im struggling with custom module. I want to use $product variable in custom module template.

That works in every product page but not in category and home page gives me debug error

Notice: Undefined index: product

like

{$product.name}

When i wrap it like

 {foreach from=$products item="product"}
     {$product.name}
 {/foreach} 

then $product variables works but i want only to show variable matched to product.

Any ideas?

Ive implemented product functions from ps_specials with all needed interfaces like

 $products = $this->getSpecialProducts();

 $this->smarty->assign(
    array(
        'products' => $products,
    )); 

 return false;

Direct me how to get $product variable in home and category pages (in hooked custom template file from custom module in {hook h='displayProductListReviews' product=$product})


Edit.

My function looks right now

 public function hookDisplayProductListReviews($params){

    $templateFile = 'test.tpl';
    $products = $this->getSpecialProducts($params['product']);
    $this->context->smarty->assign(
        array(
            'products' => $products,
        )); 
    return $this->fetch('module:'.$this->name.'/'.$templateFile);
}

so far so good because with

{foreach from=$products item=product} {$product->name} {/foreach}

i can print $product->name but...

How to use it without foreach loop that print names of all products in gathered in function getSpecialProducts (from ps_specials module)?

Upvotes: 0

Views: 3379

Answers (1)

Marcin Jaworski
Marcin Jaworski

Reputation: 340

You should do

public function hookDisplayProductListReviews($params){
    $products = $this->getSpecialProducts($params['product']);
}

When you use {hook h='displayProductListReviews' product=$product}) product=$product will be in $params, you can use print_r($params);die(); to look what exactly is in that variable

Upvotes: 2

Related Questions