Jump to content
00Jose00

Override product list query

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

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 by 00Jose00 (see edit history)

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

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

Important Information

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