Aaviya
Aaviya

Reputation: 1190

Sort by two parameters in Opencart

I want to have my products sorted by two parameters: first by "sort order" that every product has; and then by quantity.

Here is what I have in file /catalog/controller/product/category.php (Opencart 1.5.6.4) and I believe here is what should be somehow changed:

if (isset($this->request->get['sort'])) {
    $sort = $this->request->get['sort'];
} else {
    $sort = 'p.sort_order';
}

Anyone has any ideas? Thanks in advance

Upvotes: 1

Views: 479

Answers (1)

You Old Fool
You Old Fool

Reputation: 22941

By default Opencart only uses a single field at a time for sorting. To accomplish what you described you'll need to modify the query for the getProducts() method which is built in /catalog/model/catalog/product.php. Find line 196 where it reads:

$sql .= " ORDER BY p.sort_order";

This is the default sort order when none is specified. To add a secondary sort criteria you can change it to:

$sql .= " ORDER BY p.sort_order, p.quantity";

If you always want to use quantity as a secondary sort field, you can add after the entire block like this:

    } else {
        $sql .= " ORDER BY p.sort_order";   
    }

    $sql .= ", p.quantity";

This would sort by quantity as a secondary field regardless of what primary sort field was specified.

Upvotes: 2

Related Questions