Overriding the query that returns the product list in categories. Adding clause like “order by” to order this products by different values of ps_products tables, like “date_upd, active…”. Order products by “date_upd” if the category id is equal to for example 121.
Create Custom module
1) Create directory:
override/controllers/front/listing
2) In your module create directory:
modules/mycustommodule/override/controllers/front/listing
3) In your module directory (modules/mycustommodule/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
Modify / overwrite PrestaShop CategoryController class
<?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 == 121) {
$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();
}
}
}
Forum: https://www.prestashop.com/forums/topic/1007846-override-product-list-query/
hi i tested it but not work! i guess have mistaken. can you create 1.6 and 1.7 module for download? i tested in PS 1.7
best regard
protected function getProductSearchQuery()
{
if ($this->category->id == 121) {
$query = new ProductSearchQuery();
$query
->setIdCategory($this->category->id)
->setSortOrder(new SortOrder(‘product’, Tools::getProductsOrder(‘by’, ‘quantity’), Tools::getProductsOrder(‘way’,’desc’)));
} else {
$query = new ProductSearchQuery();
$query
->setIdCategory($this->category->id)
->setSortOrder(new SortOrder(‘product’, Tools::getProductsOrder(‘by’), Tools::getProductsOrder(‘way’)));
}
return $query;
}
protected function getProductSearchQuery()
{
if ($this->category->id == 121) {
$query = new ProductSearchQuery();
$query
->setIdCategory($this->category->id)
->setSortOrder(new SortOrder(‘product’, Tools::getProductsOrder(‘by’, ‘quantity’), Tools::getProductsOrder(‘way’,’desc’)));
} else {
$query = new ProductSearchQuery();
$query
->setIdCategory($this->category->id)
->setSortOrder(new SortOrder(‘product’, Tools::getProductsOrder(‘by’), Tools::getProductsOrder(‘way’)));
}
return $query;
}