-1

好的,所以我想为每个类别显示制造商徽标。制造商徽标应与类别相关,即移动电话类别应仅显示属于移动电话类别的制造商徽标,例如 Microsoft 或 Sony 的徽标不应在此处显示(移动电话类别),而是应显示在游戏机类别中。

目录/控制器/产品/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'])
);
} 

类别.tpl

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

目前它显示所有类别中的所有制造商徽标。

4

1 回答 1

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 查询,但它应该可以正常工作。

于 2013-06-22T15:06:18.430 回答