Opencart按类别页面中的制造商排序

时间:2013-10-23 18:44:09

标签: sql sorting sql-order-by opencart

好的,我想在类别页面上再添加一个排序变量,即按制造商排序。 在1.5.3.1上运行并尝试过: 1.目录/控制器/产品/ category.php 补充说:

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

$this->data['sorts'][] = array(
                'text'  => $this->language->get('text_manufacturer_desc'),
                'value' => 'manufacturer-DESC',
                'href'  => $this->url->link('product/category', 'path=' . $this->request->get['path'] . '&sort=manufacturer&order=DESC' . $url)
            );
  1. 目录/语言/ * /产品/ category.php 补充道:

    $ _ ['text_manufacturer_asc'] ='▲-Proizvođaču-▲'; $ _ ['text_manufacturer_desc'] ='▼-Proizvođaču-▼';

  2. 目录/模型/目录/ product.php 之前:'p.date_added'我插了一行:

    '制造商'

  3. 以下几行我用下面的代码更改了块:

        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)";
        } elseif ($data['sort'] == 'manufacturer') {
            $sql .= " SELECT * FROM". DB_PREFIX . "product p ORDER BY manufacturer";
        } else {
            $sql .= " ORDER BY " . $data['sort'];
        }
    } else {
        $sql .= " ORDER BY p.sort_order";   
    }
    

    但它不起作用。 错误说:

    PHP Notice:  Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROMproduct p ORDER BY manufacturer ASC,...
    

    我发现了另一个:

    PHP Notice:  Error: Unknown column 'special' in 'order clause'<br />Error No: 1054<br />SELECT p.product_id, (SELECT AVG(rating) AS total FROM review r1 WHERE r1.product_id = p.product_id AND r1.status = '1' GROUP BY r1.product_id)...
    

    有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

你几乎就在那里,但你必须再修改一下。

首先,您真的不希望按照product.manufacturer_id 中存储的制造商ID进行订购,而是按制造商名称进行订购。在这种情况下,我们需要在catalog\model\catalog\product.php方法中修改getProducts模型中的查询 - 找到这一行:

$sql .= " LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";

(应该在OC 1.5.5.1中 89 行)和 之前添加:

$sql .= " LEFT JOIN " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id";

此外,您还需要允许制造商作为排序选项:

$sort_data = array(
    'pd.name',
    'p.model',
    'p.quantity',
    'p.price',
    'rating',
    'p.sort_order',
    'p.date_added',
    'manufacturer', // <== this is added
);  

现在在排序部分按制造商名称添加排序:

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)";
    } elseif ($data['sort'] == 'manufacturer') { // <== this is added
        $sql .= " ORDER BY m.name";              // <== this is added
    } else {
        $sql .= " ORDER BY " . $data['sort'];
    }
} else {
    $sql .= " ORDER BY p.sort_order";   
}

就是这样。