A
A
Alexander Gukov2017-02-08 15:32:02
PHP
Alexander Gukov, 2017-02-08 15:32:02

How to find category id in Opencart 2 product card?

Guys, I've been struggling with this problem for a long time.
I need to display text\banner for certain categories in the product card. It is not possible to get the id in order to make a condition.
As an option, I tried to use the code for Opencart 1.5. Tried to redo it, nothing works.

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

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Shcherbokov, 2017-02-10
@drogwur

product/product.php controller

//Находим строку
if ($product_info) {
     
//Добавялем
$categories = $this->model_catalog_product->getCategories($product_id);
            
if($categories){
     $categories_info = $this->model_catalog_category->getCategory($categories[0]['category_id']); 
     //Только зачем Вам новый запрос всех данных категории? category_id уже тут $categories[0]['category_id']
     $data['category_id'] = $categories_info['category_id'];    
    //В op2
}

W
web-mechanic, 2017-02-16
@amfetamine

the id of the current category is taken from the variable $this->requet->get['path']
for example, if your variable contains 43_56_78, then the current category has id 78
, split using explode() and take the last element from the result

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question