dkjain
dkjain

Reputation: 881

What is sort_order-ASC in Opencart's category controller file?

Calling all opencart guru's. While trying to understand category page's sorting feature (sort by Name, Rating, Price etc). The line p.sort_order-ASC in catalog/controller/product/category.php baffles me. In SQL, a sort query is built similar to something like ORDER BY table.field_name ASC/DESC but I cannot deduce the logic of p.sort_order-ASC. Is it set as value to be passed to a javascript function?

$this->data['sorts'] = array();

        $this->data['sorts'][] = array(
            'text'  => $this->language->get('text_default'),
            'value' => 'p.sort_order-ASC',
            'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.sort_order&order=ASC' . $url)
        );

Upvotes: 2

Views: 2082

Answers (2)

shadyyx
shadyyx

Reputation: 16055

Actually the line

'value' => 'p.sort_order-ASC',

is used in the template file (e.g. catalog/view/theme/default/template/product/category.tpl) for highlighting (selecting) the selected sorting type. In the mentioned controller the sort and order are obtained from the GET - concretely from the URL that is hit and is defined by this line:

'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=p.sort_order&order=ASC' . $url)

As You can see, the URL contains the &sort and &order parameters the necessary values are obtained from and pushed to the template.

In the template there is this condition:

    <?php foreach ($sorts as $sorts) { ?>
    <?php if ($sorts['value'] == $sort . '-' . $order) { ?>
    <option value="<?php echo $sorts['href']; ?>" selected="selected"><?php echo $sorts['text']; ?></option>
    <?php } else { ?>

so if $sort . '-' . $order obtained from the URL is the same as the sort's value, the <option> is selected.

Is it clearer now?

Upvotes: 0

Jay Gilford
Jay Gilford

Reputation: 15151

OpenCart takes the value you have there and sorts it based on those, so it orders by p.sort_order (which is the product table's alias p and the sort_order field) followed by the way it is sorted which is ASC or ascending

Upvotes: 3

Related Questions