2013-06-21 73 views
-1

好吧,我想顯示每個類別的製造商徽標。製造商徽標應與類別相關,即手機類別應僅顯示屬於類別的手機的製造商徽標,例如Microsoft或Sony的徽標不應顯示在此處(手機類別),而應顯示在遊戲控制檯類別。OpenCart - 在類別中顯示製造商徽標頁面

目錄/控制器/產品/ category.php

$this->load->model('catalog/manufacturer'); 

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

    $results = $this->model_catalog_manufacturer->getManufacturers(); 
    foreach ($results as $result) { 
     $this->data['manufacturers'][] = array(
     'manufacturer_image' => $this->model_tool_image->resize($result['image'],80,80), 
     'name' => $result['name'], 
     'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']) 
); 
} 

category.tpl

<?php foreach ($manufacturers as $manufacturer) { ?> 
<img src="<?php echo $manufacturer['manufacturer_image']; ?>" /> 
<?php } ?> 

目前,它示出了在所有類別的所有制造商的標誌。

+0

您需要通過傳遞'category_id'來過濾來自'getManufacture'的結果,此時函數不知道類別上下文。 – DevZer0

+0

只是想知道爲什麼*索尼*不應該顯示在手機內(因爲我們有*索尼愛立信*到2012年,現在它只是,只有*索尼*品牌)...^_^ – shadyyx

+0

1.什麼版本的OC你正在用嗎? 2.您如何指定**類別** <=> **製造商**之間的關係?我猜你應該能夠將製造商與某個類別聯繫起來,然後只是選擇所有與之相關的東西......或者你有人可以創建這樣一種智能算法,以使PHP知道*微軟製作Xbox - 讓我們展示它遊戲控制檯類別!* – shadyyx

回答

1

OpenCart的股票製造商模型不提供這種功能,所以你需要創建一個新的方法。在/catalog/model/product/manufactuer.php添加

public function getManufacturersByCatgoryId($category_id) { 


    $query = $this->db->query(" 
     SELECT 
      m.* 
     FROM 
      " . DB_PREFIX . "product_to_category p2c 
      LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id) 
      LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) 
      LEFT JOIN " . DB_PREFIX . "manufacturer m ON (m.manufacturer_id = p.manufacturer_id) 
      LEFT JOIN " . DB_PREFIX . "manufacturer_to_store m2s ON (m2s.manufacturer_id = p.manufacturer_id) 
     WHERE 
      p2c.category_id = '" . (int) $category_id . "' 
      AND m2s.store_id = '" . (int) $this->config->get('config_store_id') . "' 
      AND p.status = 1 
      AND p2s.store_id = '" . (int) $this->config->get('config_store_id') . "' 
     GROUP BY 
      m.manufacturer_id 
    "); 

    return $query->rows; 

} 

然後,您可以通過在控制器中這樣獲取並使用你已經得到的代碼將它傳遞給視圖:

$this->load->model('product/manufactuer'); 
$manufacturers = $this->model_product_manufacturer->getManufacturersByCategoryId($category_id); 

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

foreach ($manufacturers as $manufacturer) { 
    $image = $manufacturer['image']; 
    $this->data['manufacturers'][] = array(
      'manufacturer_image' => $this->model_tool_image->resize($result['image'],80,80), 
      'name' => $result['name'], 
      'href' => $this->url->link('product/manufacturer/info', 'manufacturer_id=' . $result['manufacturer_id']) 
    ); 

} 

我沒有測試上面的代碼,你可能可以優化一些SQL查詢,如果你想,但它應該可以正常工作。

相關問題