Jump to content

In-stock product first on the searched list


freelex

Recommended Posts

Hello, I'm trying to set up so that when I search for products, in stock products are displayed first and only those that are out of stock are below them. I managed to set it up for searching through categories. However, I did not manage to find out which file I need to modify in order for my products to be sorted in this way even when searching through the search button.

I use Prestashop 1.7.7.5.


I changed the below line in the \modules\ps_facetedsearch\src\Adapter\MYSQL.php

Orginal:

        if ($orderField) {
            $query .= ' ORDER BY ' . $orderField . ' ' . strtoupper($this->getOrderDirection());
        }

Modified:

        if ($orderField) {
            $query .= ' ORDER BY (CASE WHEN quantity > 0 THEN 1 ELSE 0 END) DESC, ' . $orderField . ' ' . strtoupper($this->getOrderDirection());
        }

Which other file do I need to modify?

Thank you for your help.

Link to comment
Share on other sites

Displaying the product sheet when I open Category1 (for example Chocolate category) works great after modification the file  \modules\ps_facetedsearch\src\Adapter\MYSQL.php.

If I search via the button search for Chocolate, it doesn't work and products that are out of stock are also displayed first. I think another file than \modules\ps_facetedsearch\src\Adapter\MYSQL.php will need to be edited too.

  • Like 1
Link to comment
Share on other sites

e.g.:

class Search extends SearchCore 
{
    public static function find(
        $id_lang,
        $expr,
        $page_number = 1,
        $page_size = 1,
        $order_by = 'position',
        $order_way = 'desc',
        $ajax = false,
        $use_cookie = true,
        Context $context = null
    ) {
...
...
/* find */
if ($order_by !== 'price') {
            $sql .= ($order_by ? ' ORDER BY  ' . $alias . $order_by : '') . ($order_way ? ' ' . $order_way : '') . '
				LIMIT ' . (int) (($page_number - 1) * $page_size) . ',' . (int) $page_size;
        }

        $result = $db->executeS($sql, true, false);

        if ($order_by === 'price') {
            Tools::orderbyPrice($result, $order_way);
            $result = array_slice($result, (int) (($page_number - 1) * $page_size), (int) $page_size);
        }
/* change to ORDER BY stock.quantity ......

 

  • Like 1
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...