Jump to content

How to extend Faceted Search productSearchProvider


fedesib

Recommended Posts

Hi there,

is there a proper way to extend Faceted Search productSearchProvider?

I need to modify the getQuery function inside ps_facetedsearch/src/Adapter/MySQL.php and I did not find a way to do it properly: I know it can be done by writing my own productSearchProvider but for a simple modification such as mine it would be like reinventing the wheel.

I also tried the hooks actionProductSearchProviderRunQueryBefore and actionProductSearchProviderRunQueryAfter but they are not what I need.

Thanks a lot if you can give me some advice.
Federica

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

Since you want to modify the getQuery function inside the ps_facetedsearch/src/Adapter/MySQL.php file, I suggest using the event-driven approach provided by PrestaShop. While there might not be a direct hook that suits your needs, you can still use events to achieve your goal.

In your custom module, you can override the MySQL class with your own implementation. You can do this by creating a class that extends the original MySQL class and overrides the getQuery function as needed.

For example :

// custommodule/Adapter/MySQL.php
require_once _PS_MODULE_DIR_.'custommodule/vendor/autoload.php';

use Ps_Facetedsearch\Adapter\MySQL as OriginalMySQL;

class CustomMySQL extends OriginalMySQL
{
    public function getQuery($params)
    {
        // Your custom implementation here
    }
}

The, You need to register your override in your module's install method using the overrideClass method.

public function install()
{
    return parent::install() && $this->registerOverride();
}

private function registerOverride()
{
    return Db::getInstance()->execute(
        'INSERT INTO '._DB_PREFIX_.'override 
        (`id_module`, `id_shop`, `id_employee`, `class_name`, `file_name`, `type`) 
        VALUES ('.(int)$this->id.', 0, 0, 
        "Ps_Facetedsearch\Adapter\MySQL", 
        "Adapter/MySQL.php", 
        "class")'
    );
}

Inside your CustomMySQL class, you can implement your custom logic in the getQuery function.

If there are no direct hooks available that match your requirement, you can use existing hooks in conjunction with event subscribers to achieve your goal. You can create an event subscriber that listens for certain hooks and then calls the modified getQuery function.

Thoroughly test your customizations to ensure they work as expected.

Remember that this approach involves some complexity, but it allows you to extend and customize the core functionality without modifying core files directly.

Let me know If it helps!

Thanks!

Link to comment
Share on other sites

Hi,

thank you very much for answering my question and for giving me directions!

I tried to override the original getQuery function inside MySQL class, but I couldn't reach it properly and I did not find any explanation about how to do it.

Thank you again and I'll let you know.

All the best,
Federica

Link to comment
Share on other sites

in order to modify the getQuery function within the ps_facetedsearch/src/Adapter/MySQL.php file, I recommend leveraging the event-driven approach provided by PrestaShop. While there might not be a direct hook available that perfectly aligns with your requirements, you can effectively achieve your objective by utilizing events.

By using PrestaShop's event system, you can intercept and manipulate the necessary data or logic without directly modifying the core files. This approach helps maintain the integrity of your PrestaShop installation and ensures smoother updates in the future.

Although there might not be a predefined hook that directly fits your use case, you can identify relevant events within the application's workflow and then register your custom code to listen for those events. This allows you to execute your modifications at the appropriate point in the execution flow.

Remember to thoroughly understand the available events within PrestaShop, choose the event that closely aligns with the context you're working in, and implement your modifications accordingly. This event-driven approach will help you achieve your goal while adhering to best practices and preserving the flexibility of your PrestaShop installation. For more details, visit.. 

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

Hi again,

I tried your suggestions but I got stuck in the override registration process, since there is no ps_override table in the DB (I'm working on PS 1.7.8.x but as far as I know also PS 8.x does not have such a DB table) and to my knowledge the override process does not need any insert in the DB because override infos are discovered by PS during the installation of the module using paths in the override folder of the module itself (docs here: https://devdocs.prestashop-project.org/1.7/modules/concepts/overrides/).

Trying to override ps_facetedsearch/src/Adapter/MySQL.php using mymodule/override/ps_facetedsearch/src/Adapter/MySQL.php does not work.

Maybe I'm missing something obvious here: any more suggestions?

Thanks,
Federica

Edited by fedesib (see edit history)
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...