2

我在latest_content.tpl文件中使用此代码来获取类别名称。但它不显示类别名称。如何在 opencart 中获取类别名称。

 $categories = $this->model_catalog_product->getCategories($product_id);
if ($categories)
   $categories_info = $this->model_catalog_category->getCategory($categories[0]['category_id']);
$this->data['category_title'] = $categories_info['name'];
echo echo $category_title;
4

1 回答 1

3

catalog/controller/module/latest.php, 之前$this->data['products'][] = array(添加:

$categories = $this->model_catalog_product->getCategories($result['product_id']);
if($categories){
    $categories_info = $this->model_catalog_category->getCategory($categories[0]['category_id']);
    $category_title = $categories_info['name'];
}else{
    $category_title = '';
}

更新$this->data['products'][]数组如下:

 $this->data['products'][] = array(
            'product_id' => $result['product_id'],
            'category_title' => $category_title,
            'thumb'      => $image,
            'name'       => $result['name'],
            'price'      => $price,
            'special'    => $special,
            'rating'     => $rating,
            'reviews'    => sprintf($this->language->get('text_reviews'), (int)$result['reviews']),
            'href'       => $this->url->link('product/product', 'product_id=' . $result['product_id']),
        );

现在在 中latest.tpl,您将获得类别标题为$product['category_title']

附加信息: - 模型函数应在控制器中调用。控制器中定义的变量$this->data['variable_name']可以在模板文件中以$variable_name.

祝你今天过得愉快 !!

于 2014-03-04T10:31:29.527 回答