Today let’s talk about the methods which can help us draw customers attention to discounted products. We suggest placing them at the top of the directory and not changing their place even if a customer applies any kind of sorting, which will attract additional visitors’ attention.
So, for example, we have a directory with 2 discounted items:
In order to aply additional type of sorting we will need to rewrite the Category class, that is to modify the file classes/category.php by adding the method “reduсedCmp” and modify the method Category::getPrоduсts()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null) { ........ //* Modify SQL result */ //return Product::getProductsProperties($id_lang, $result); //sorting the collectiong of products $product_collection = Product::getProductsProperties($id_lang, $result); usort($product_collection, array("Category", "reducedCmp")); //sorting by the parameter 'reduction' return $product_collection; // returning the modified result } public function reducedCmp($a, $b) { if ($a['reduction'] == $b['reduction']) { return 0; } return ($a['reduction'] > $b['reduction']) ? -1 : 1; } |
Now the directory page looks like this:
If you want to apply this sorting only when the default sorting is used, you need to replace the line:
1 |
usort($product_collection, array("Category", "reducedCmp")); |
to this line
1 2 3 |
if (#order_by == “position”) { usort($product_collection, array("Category", "reducedCmp")); } |
If you are using the module Blосklаyered, then you need to further edit the function BlосkLаyered::getPrоduсts() in the file modules/blосklаyered/blосklаyered.php by adding this line at the end:
1 |
usort($product_collection, array("Category", "reducedCmp")); |
PrestaShop Development
Take your online store to the next level with BelVG PrestaShop Development
Visit the page
lordbdp,
OK, thanks for this addition!
When I activate the Debug mode I have an error :
Strict Standards: usort() expects parameter 2 to be a valid callback, non-static method Category::reducedCmp() should not be called statically, assuming $this from compatible context Category in /home/my_website_com/override/classes/Category.php on line 669
It correspond to the usort line :
//* Modify SQL result */
//return Product::getProductsProperties($id_lang, $result); //sorting the collectiong of products
$product_collection = Product::getProductsProperties($id_lang, $result);
usort($product_collection, array(“Category”, “reducedCmp”)); //sorting by the parameter ‘reduction’
return $product_collection; // returning the modified result
}
It look like nice now tks a lot.
Before, by default I had the new products in 1st. With this modification I have the Discounted products in 1st, but how add the new products just after the discounted now plz ?
error Found ! ^^
this is the good code :
protected function showPriceFilter()
{
return Group::getCurrent()->show_prices;
usort($product_collection, array(“Category”, “reducedCmp”));
}
}
lordbdp,
Please contact our support team at [email protected] and they will help you.
On Category.php I have now it :
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
if ($order_by == ‘orderprice’)
Tools::orderbyPrice($result, $order_way);
if (!$result)
return array();
//* Modify SQL result */
//return Product::getProductsProperties($id_lang, $result); //sorting the collectiong of products
$product_collection = Product::getProductsProperties($id_lang, $result);
usort($product_collection, array(“Category”, “reducedCmp”)); //sorting by the parameter ‘reduction’
return $product_collection; // returning the modified result
}
public function reducedCmp($a, $b) {
if ($a[‘reduction’] == $b[‘reduction’]) {
return 0;
}
return ($a[‘reduction’] > $b[‘reduction’]) ? -1 : 1;
}
/**
* Return main categories
And in blocklayered.php :
protected function showPriceFilter()
{
return Group::getCurrent()->show_prices;
}
}
usort($product_collection, array(“Category”, “reducedCmp”));
But nothing new at the screen now….