我在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;
答案 0 :(得分:1)
在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
访问。
有一个niceday !!