Jump to content

Override product list query


00Jose00

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

Link to comment
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

Link to comment
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();           
        }
  }
}

 

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

  • 1 year later...

Hello,

I wonder how to make a custom sql query in the override/controllers/front/listing/CategoryController.php page.
If I use $query->leftJoin() or/and $query->where() methods, it fails.

IA told me to do this:

use Doctrine\ORM\AbstractQuery;
class MyQuery extends AbstractQuery { }
$query = new MyQuery();

But it still does not work.
Any help would welcome :)

Thank you.

Link to comment
Share on other sites

  • 3 weeks later...
On 4/29/2022 at 5:44 PM, neoweiter said:

Does anywone knows how to add a custom WHERE clause to getProductSearchQuery();

Hi,

 

->setSortOrder(new SortOrder('product', Tools::getProductsOrder('by', 'date_upd'), Tools::getProductsOrder('way', 'desc')))

->addWhere('p.date_upd IS NOT NULL');

Thanks!

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