Jump to content

How to manipulate the query that fetches products to display on product lists (mainly category page).


Recommended Posts

I need to adapt the query that fetches products for the category page (add a join to a custom table, and some where clauses).

I've tried to find a hook that would allow me to change the query. I did find hookActionProductSearchProviderRunQueryBefore but the $params['query'] in this hook seems to just give the query parameters as set by the faceted search module, and the actual query can't be changed there, nor can I change $params['query'] to add a join and where clauses.

How would I accomplish this?

 

Link to comment
Share on other sites

  • 1 month later...
On 3/26/2025 at 5:29 PM, zunxunz said:

I need to adapt the query that fetches products for the category page (add a join to a custom table, and some where clauses).

I've tried to find a hook that would allow me to change the query. I did find hookActionProductSearchProviderRunQueryBefore but the $params['query'] in this hook seems to just give the query parameters as set by the faceted search module, and the actual query can't be changed there, nor can I change $params['query'] to add a join and where clauses.

How would I accomplish this?

 

Hello,

You need to make changes in the ps_facetedsearch module files. You can modify the getQuery function of MySQL.php in ps_facetedsearch module. 

Link to comment
Share on other sites

  • 4 weeks later...
Posted (edited)

Is there no way to 'hook' into the code that builds the query?
Prestashop keeps discouraging modifying core code or using overrides, and keeps promising to add more 'hooks' to allow extending or changing functionality, but at the same time keeps wrapping code in more layers of code wrapped in layers of code, and keeps making it more and more complex to customize the code through hooks or proper class or module extensions.

To mildly say it, this is seriously 'not very nice'.
 

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

Decorate/Extend the Faceted Search Provider Service 

This approach lets you extend the original class and only override the functions you want.

Downside: Any major update to the ps_facetedsearch module could break your changes, but this is true for most deep customizations.

Here is link to example module

 

 

 

Link to comment
Share on other sites

Posted (edited)

Thank you for your answer, but it does not seem to work.

I used the module you provided to test it.


It raises a Symphony exception saying: 
The service "myfacetedsearchoverride.facetedsearch.adapter.mysql" has a dependency on a non-existent service "prestashop.module.facetedsearch.adapter.mysql".

And I don't see that service being registered in the ps_facetedsearch module anywhere.

I'm working on Prestashop v8 by the way.

 

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

Posted (edited)

then you should try autoload

https://devdocs.prestashop-project.org/8/modules/concepts/composer/#autoloading

 

How Composer/Class Autoloading Works in PrestaShop

  • Composer’s autoloader loads classes according to autoload rules (PSR-4, classmap, etc.).
  • If two class definitions are found for the same FQCN (Fully Qualified Class Name), the first one registered wins.
  • PrestaShop’s native override system (/override/classes/...) only works for core classes, not module classes, and not for Composer-loaded namespaces.
  • Modules with Composer (like ps_facetedsearch) register their own PSR-4 autoloaders, which makes “classic” PrestaShop override impossible.

Place Your File Somewhere Higher in the Autoload Chain

There are several ways to do this, depending on your project setup:

A. Use a Custom Module with Composer Autoloading

Create your own module, e.g. myfacetedsearchoverride

In your module, create the folder:
src/PrestaShop/Module/FacetedSearch/Adapter/

Add your own MySQL.php with the exact same namespace and class name.

In your module’s composer.json:

{
  "autoload": {
    "psr-4": {
      "PrestaShop\\Module\\FacetedSearch\\": "src/PrestaShop/Module/FacetedSearch/"
    }
  }
}
 

Run composer dump-autoload in the root of your PrestaShop install so that your autoloader is registered before the module’s.

When your autoloader is registered first, your class will be loaded instead of the original.

B. Modify the Main Shop’s Composer Autoload (If You Have Access)

  • If you have access to the main composer.json in your shop root,
  • add a classmap or files autoload entry that points to your custom class

Place your custom file at that location, using the exact same namespace.

Run composer dump-autoload to regenerate.

example module:  

https://drive.google.com/file/d/1fYbzajiME4cmXxhqI3-sHgLnZgYl8H6N/view?usp=sharing

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

That solution might work, but it would seem that can not be implemented just by installing a module?

If I understand you correctly, It would require to completely rebuild the core's autoload manually. This would essentially mean I'd be overwriting the core code, but that is what I'm trying to avoid.

 

Link to comment
Share on other sites

1 hour ago, zunxunz said:

That solution might work, but it would seem that can not be implemented just by installing a module?

If I understand you correctly, It would require to completely rebuild the core's autoload manually. This would essentially mean I'd be overwriting the core code, but that is what I'm trying to avoid.

 

If you’re planning to develop a module for distribution, then modifying Faceted Search may be the most practical—if not ideal—approach.

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