freelex Posted September 21, 2022 Share Posted September 21, 2022 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 More sharing options...
ps8modules Posted September 22, 2022 Share Posted September 22, 2022 (edited) replace quantity to sa.quantity If you look in the MySQL.php file, you will see the prefix and name below. Edited September 22, 2022 by 4you.software (see edit history) 1 Link to comment Share on other sites More sharing options...
freelex Posted September 22, 2022 Author Share Posted September 22, 2022 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. 1 Link to comment Share on other sites More sharing options...
ps8modules Posted September 22, 2022 Share Posted September 22, 2022 Yes, ./classes/Search.php needs to be edited 1 Link to comment Share on other sites More sharing options...
ps8modules Posted September 22, 2022 Share Posted September 22, 2022 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 ...... 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now