Reputation: 114
How can i sort my products in opencart as like below,
All the products will be sorted by order, but out of stock products(which means if quantity is = 0) will be listed at the end of the category!
Upvotes: 0
Views: 1572
Reputation: 106
In opencart 2.2 you have to change catalog\model\catalog\product.php
find
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY LCASE(" . $data['sort'] . ") ";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} else {
$sql .= " ORDER BY " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.sort_order";
}
and add additional order rule p.quantity<1 after "ORDER BY"
if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
if ($data['sort'] == 'pd.name' || $data['sort'] == 'p.model') {
$sql .= " ORDER BY p.quantity<1, LCASE(" . $data['sort'] . ") ";
} elseif ($data['sort'] == 'p.price') {
$sql .= " ORDER BY p.quantity<1, (CASE WHEN special IS NOT NULL THEN special WHEN discount IS NOT NULL THEN discount ELSE p.price END)";
} else {
$sql .= " ORDER BY p.quantity<1, " . $data['sort'];
}
} else {
$sql .= " ORDER BY p.quantity<1, p.sort_order";
}
Upvotes: 3
Reputation: 3875
You can have 2 queries. One in which quantity != 0 ORDER BY sort_order
and second quantity = 0
. Then you can merge the records and have your desired output.
Upvotes: 0