2013-10-23 79 views
0

好的,我想在類別頁面上添加一個排序變量,即按製造商排序。 運行在1.5.3.1並試圖這樣: 1.目錄/控制器/產品/ category.php 加入下列:Opencart按類別製造商排序頁面

$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' 我插入的線:

    '製造商'

和幾行下面我與改變了塊如下:

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

你幾乎在那裏,但你將不得不修改它多一點。

首先,您確實不希望通過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)和之前將其添加:

$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"; 
} 

就是這樣。

+0

感謝您的幫助。現在沒有錯誤,但它默認排序。我正在爲這個改變做一個vqmod,所以我現在才發現我錯過了你所引用的OC 1.5.5.1的89行......在1.5.3.1中沒有這樣的行 - 看起來似乎。 – user1688303

+0

然後你需要找到放置這條新線的地方。它必須位於'LEFT JOIN's和'WHERE'子句之間...它之間沒有這樣的空間來填充該行,然後選擇SQL並將其放入獲取產品的主要SQL部分(我相信您瞭解SQL語法和子句)。 – shadyyx

+0

好吧,vqmod沒有在catalog/model/catalog/product.php文件中進行更改,所以我確實將其更改爲manualy。現在有一個錯誤說:PHP注意:錯誤:'訂單子句中的未知列'm.name' – user1688303