Jump to content

Help using hookFilterProductSearch


Recommended Posts

Hello !

I have a problem using an undocumented hook. As far as I googled, there is no informations on how to use the 'filterProductSearch' hook. More precisely, once I've filtered the products I wanted into an array, how do I communicate it back to prestashop ?

Sorry if the question sounds dumb, but it's my first project using PS, which is a great framework by the way, but the lack of reliable documentation makes it really tough once you want to do advanced stuff. 

Cheers ❤️

Link to comment
Share on other sites

Thank you so much for the answer ! But I stumbled on that topic and that's what I tried, but to no effect, so I'm probably doing something wrong...

My hook as it is :

Quote

public function hookFilterProductSearch(&$params){
        $product_array = array(
            0 => $params['searchVariables']['result']->getProducts()[1]
        );
        $this->logObject("Product array", $product_array);
        $params['searchVariables']['result']->setProducts($product_array)->setTotalProductsCount(count(($product_array)));
    }

The log is as follows, so the new array seems to be all right :

Quote

[19:45:23] Product array : array (
  0 => 
  array (
    'id_product' => '24',
    'id_supplier' => '0',
    'id_manufacturer' => '0',
    'id_category_default' => '2',
    'id_shop_default' => '1',
    'id_tax_rules_group' => '1',
    'on_sale' => '0',
    'online_only' => '0',

etc.

However, the result is still displaying all of my three dummy products when it should ideally display one.

Once again, thanks for your help, not super familiar to PHP so might have done a stupid error somewhere.

Edited by tatoufff
Typo in the code, changes nothing to the result (see edit history)
Link to comment
Share on other sites

Okay, so I tried to debug it myself, and went into the PHP source files to track out the search variables after hook execution, and it turns out the array is modified as it should. 

So the problem isn't the transmission of the array to the system, but most likely that what I'm modifying in the array is not what is affecting the end search result. So I'm coming back to my original question : what is the standard way to modify search results from the 'filterProductSearch' hook ? Does ANYONE have an example or something ? 

In the meantime guess I'll try to go and find answers in the source code. But for the love of god, that kind of stuff should really be documented, at least with a simple usage example... Anyways if I find anything I'll make sure to post it here for legacy.

Link to comment
Share on other sites

Okay, finally got a solution. 

As hinted here, to achieve a custom search you have to use the ProductSearchProvider hook. After registering the hook and importing the right classes, I implemented it like this

public function hookProductSearchProvider($params){
    error_log("Hey from hookProductSearchProvider !");
    return new CustomSearchEngine();
}

Then I created CustomSearchEngine by implementing the ProductSearchProviderInterface as follows :

class CustomSearchEngine implements ProductSearchProviderInterface{
    public function runQuery(ProductSearchContext $context, ProductSearchQuery $params){
        $new_products = new ProductSearchResult();
        $array_list = array(array('id_product' => 24));
        $new_products->setProducts($array_list);
        return $new_products;
    }
}

As you can see, I haven't yet done the database querying part, but the gist is here : you create a ProductSearchResult object, and you fill it with an array containing the IDs of the products of your choice, obtained via query. You don't have to fill any extra informations, the core will automagically complete the product list with all the info it needs.

 

Just an afterword because I'm kinda salty, feel free to ignore but I have to get it out of my chest : Prestashop is a really good tool, and I get you have limited ressources, but PLEASE that kind of process should be documented adequately. Going through the source code and litter it with debug breakpoints to figure out how an official Dev API works is not how it should be. I'll contribute to the main doc on the site for that topic, but I feel like ultimately it isn't up to the community to contribute to basic documentation edited by a private company, even if it bases its model around free software mentality. I'd rather have a somewhat slower rate of novelty for a decent up-to-date documentation.

  • Like 4
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...