Answer the question
In order to leave comments, you need to log in
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
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
}
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 questionAsk a Question
731 491 924 answers to any question