Jump to content

Override product list query


00Jose00
 Share

Recommended Posts

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

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

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

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

  • 2 years later...
  • 2 months later...
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

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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More