How to Display Discounted Products at the Top of the Directory in Prestashop

How to Display Discounted Products at the Top of the Directory in Prestashop

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:

1Discounted Products

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()

Now the directory page looks like this:

2Discounted Products

If you want to apply this sorting only when the default sorting is used, you need to replace the line:

to this line

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:



6 comments

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

  2. error Found ! ^^

    this is the good code :

    protected function showPriceFilter()
    {
    return Group::getCurrent()->show_prices;
    usort($product_collection, array(“Category”, “reducedCmp”));
    }
    }

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

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

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *