2

I am using this code in latest_content.tpl file to get category name. But it is not displaying category name. How can I get category name in 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;
user3190238
  • 43
  • 1
  • 2
  • 6

1 Answers1

3

In catalog/controller/module/latest.php, before $this->data['products'][] = array( add:

$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 = '';
}

Update $this->data['products'][] array as below:

 $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']),
        );

Now in latest.tpl, you'll get the category title as $product['category_title'].

Additional information: - Model functions should be called within controllers. The variables defined $this->data['variable_name'] in controller can be accessed in template file as $variable_name.

Have a niceday !!

Sankar V
  • 4,110
  • 5
  • 28
  • 52