Category Page Product Filter

2019-08-27 10:12发布

问题:

I am new to prestashop and I am trying to create a simple product filter to be displayed on the category page. I managed to output my filter on the page using the hook for the hookDisplayLeftColumn method, however I have a few questions. Right now I am hooking to the leftColumn but the filter will show on any page that has it.I want to show it only on the category page.

public function hookDisplayLeftColumn($params)
    {

        $data = array(
            'bar' => 'foo'
        );

        $this->context->smarty->assign($data);
        return $this->display(__FILE__, 'categoryfilter.tpl');
    }

And this is the tricky part.How do I filter the products. Is there any method I can hook to and filter the results?

回答1:

If you want to only include your code in category pages use something like:

public function hookDisplayLeftColumn($params)
{
    if (!isset($this->context->controller->php_self) or $this->context->controller->php_self != 'category')
         return false;

     $data = array(
         'bar' => 'foo'
     );

     $this->context->smarty->assign($data);
     return $this->display(__FILE__, 'categoryfilter.tpl');
}


回答2:

You can hook to actionProductListOverride.

Hook is executed in CategoryController

As you can see you get three properties in params array. Because they are passed by reference you can assign them your own filtered product list and CategoryController will have your filtered data.

Make sure you set hookExecuted to true, also the data structure of catProducts should match with the one that CategoryController normally generates and nbProducts should have the total count of your filtered products.

For the first part of your question, sadlyblue gave you the answer.