00Jose00 Posted November 5, 2019 Posted November 5, 2019 Hello, I'm triying to override the query that returns the product list in categories. I need to add a clause like "order by" to order this products by different values of ps_products tables, like "date_upd, active...". I found the code in "/classes/Search.php" but when I override it, the changes are not made effect. Is the correct file? I'm triying to order this products by "date_upd" if the category id is equal to 545 (for example). Thanks in advance Share this post Link to post Share on other sites More sharing options...
EvaF Posted November 5, 2019 Posted November 5, 2019 1) create directory: override/controllers/front/listing 2) in your module create directory: modules/yourmodule/override/controllers/front/listing 3) in this directory (modules/yourmodule/override/controllers/front/listing) create file CategoryController.php with this content: <?php /** * 2007-2018 PrestaShop. * *** */ use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use PrestaShop\PrestaShop\Core\Product\Search\SortOrder; class CategoryController extends CategoryControllerCore { protected function getProductSearchQuery() { $query = new ProductSearchQuery(); $query ->setIdCategory($this->category->id) ->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by', 'date_upd'), Tools::getProductsOrder('way','desc'))) ; return $query; } } 4) reinstall your module Share this post Link to post Share on other sites More sharing options...
00Jose00 Posted November 6, 2019 Posted November 6, 2019 (edited) Thanks for response @EvaF It's necessary to create a module? I only want to add a conditional like "if category id is equal to X, order by date_upd", else "order by position" in query statement. Thanks. Edited November 6, 2019 by 00Jose00 (see edit history) Share this post Link to post Share on other sites More sharing options...
EvaF Posted November 6, 2019 Posted November 6, 2019 I would create own module. It is a good way to keep your changes (overrides, hooks, etc) together - it is convenient when you upgrade prestashop to the higher version you can do it: <?php /** * 2007-2018 PrestaShop. * *** */ use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use PrestaShop\PrestaShop\Core\Product\Search\SortOrder; class CategoryController extends CategoryControllerCore { protected function getProductSearchQuery() { if ($this->category->id == somevalue) { $query = new ProductSearchQuery(); $query ->setIdCategory($this->category->id) ->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by', 'date_upd'), Tools::getProductsOrder('way','desc'))) ; return $query; } else { return parent::getProductSearchQuery(); } } } Share this post Link to post Share on other sites More sharing options...
00Jose00 Posted November 6, 2019 Posted November 6, 2019 4 hours ago, EvaF said: I would create own module. It is a good way to keep your changes (overrides, hooks, etc) together - it is convenient when you upgrade prestashop to the higher version you can do it: <?php /** * 2007-2018 PrestaShop. * *** */ use PrestaShop\PrestaShop\Core\Product\Search\ProductSearchQuery; use PrestaShop\PrestaShop\Core\Product\Search\SortOrder; class CategoryController extends CategoryControllerCore { protected function getProductSearchQuery() { if ($this->category->id == somevalue) { $query = new ProductSearchQuery(); $query ->setIdCategory($this->category->id) ->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by', 'date_upd'), Tools::getProductsOrder('way','desc'))) ; return $query; } else { return parent::getProductSearchQuery(); } } } Thank you a lot @EvaF it works like a charm!. Finally I added it into override/controllers/front/listing Share this post Link to post Share on other sites More sharing options...
neoweiter Posted April 29, 2022 Posted April 29, 2022 Does anywone knows how to add a custom WHERE clause to getProductSearchQuery(); 1 Share this post Link to post Share on other sites More sharing options...
Ali Samie Posted July 18, 2022 Posted July 18, 2022 On 4/29/2022 at 8:14 AM, neoweiter said: Does anywone knows how to add a custom WHERE clause to getProductSearchQuery(); I am actually looking for this. It should be some where. please let me know if you have found this. Thanks in advanced Share this post Link to post Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now