How can I get the product list on header?

651 views Asked by At

Hi I'm trying to do my first module.

I need to get a copy or the same $products array when I'm located on category page.

I have this code.

public function hookHeader($params)
{

    if ('category' == $this->context->controller->php_self){

    $products = //Here I need the same products array from this category

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

    return $this->display(__FILE__, 'views/templates/hook/header.tpl');
}

Thanks!

2

There are 2 answers

1
NemoPS On BEST ANSWER

Try this

$category = new Category (Tools::getValue('id_category'):
$products = $category->getProducts($this->context->lang->id, 0 99);

This will grab 99 products out of the category.

Why can't you 'copy' the category's products? SImply because they have not been assigned yet when you call hookHeader, as the CategoryController comes after that

Also, be aware that will display content in the portion, and if you want to show anything you must use hookDisplayTop

Also, in 1.6 you should use $this->context->smarty :)

0
gskema On

You should:

  1. Check if the list is already available in smarty array: error_log(print_r($this->smarty,1));
  2. Use a cateogry-page specific hook that already passes product array to you (it depends on what you need to do). If you need to add something to header, that this is probably the only hook for you. Otherwise, go to Hook.php find exec( method, add error_log($hook_name) and see whcih hooks are executed when you open category page. There may be a hook that will suit your needs.
  3. Use a static Product:: or Category:: function to grab all the products yourself.