OpenCart - 在“类别”页面中显示制造商徽标

时间:2013-06-21 02:58:32

标签: php mysql opencart

好的,所以我想显示每个类别的制造商徽标。制造商徽标应与类别相关,即移动电话类别应仅显示属于移动电话类别的制造商徽标,如此处不应显示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'])
);
} 

category.tpl

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

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

1 个答案:

答案 0 :(得分: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查询,但它应该可以正常工作。