Opencart product Category IDs on category page

371 Views Asked by At

I have question how to show product category IDs (all cat IDs for given product) on category page. Because now I am able to get only main one /top /first added.

I am using this: {{product.category_id}} in category.twig page.

Opencart 3.0.2.0

Thank you for help

PS: I was searching a lot, but all reference is either for product page (where I have it and where it works) or to show only one last category ID which I have as well. But I need ALL assigned category IDs for product on category page. So all links like this: Opencart get category_id in category page won't help.

I see I was downvoted so let me try to explain it a bit more. I was trying this code inside category.php:

            $product_category = $this->model_catalog_product->getCategories($result['product_id']);
            foreach ($product_category as $prodcat) {
                $category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
                if ($category_info) {
                    $this->data['catprod'][] = array(
                        'name' => $category_info['name'],
                        'id' => $category_info['category_id']
                    );
                }
            }

and below I have:

            $data['products'][] = array(
                'product_id'  => $result['product_id'],
                'thumb'       => $image,
                'category_id' => $category_info['category_id'],
                .....

And inside category.twig I have:

            cat_{{product.category_id}} {% for catprod in catp %}cat_{{catprod}} {% endfor %}

But this is only giving me one category ID, not all IDs.

1

There are 1 best solutions below

0
On BEST ANSWER

Tested in opencart 3.0.3.6 but should work in your version

file: catalog/controller/product/category

Before products are added

$data['products'][] = array(
// get all categories for product
$product_categories = array ();
$product_category = $this->model_catalog_product->getCategories($result['product_id']);
foreach ($product_category as $prodcat) {
    $category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
    if ($category_info) {
        $product_categories[] = array(
            'name' => $category_info['name'],
            'id' => $category_info['category_id']
        );
    }
}
$data['products'][] = array(

// rest of the product data
)

Add category array to product, anywhere within $data['products'][] = array( block

// get all categories for product
$product_categories = array ();
$product_category = $this->model_catalog_product->getCategories($result['product_id']);
foreach ($product_category as $prodcat) {
    $category_info = $this->model_catalog_category->getCategory($prodcat['category_id']);
    if ($category_info) {
        $product_categories[] = array(
            'name' => $category_info['name'],
            'id' => $category_info['category_id']
        );
    }
}
$data['products'][] = array(
                    'product_id'  => $result['product_id'],
                    'product_categories' => $product_categories,
                    // rest of the product data
                );

in category.twig within product loop you can access all categories

<ul>
  {% for cat in product.product_categories %}
  <li>{{ cat.id }} - {{ cat.name }}</li>
  {% endfor %}
</ul>

Have not fully tested it as i am using different theme but it should work. good luck