类别页面上的 Opencart 产品类别 ID

Opencart product Category IDs on category page

我对如何在类别页面上显示产品类别 ID(给定产品的所有类别 ID)有疑问。因为现在我只能得到主要的 /top /first added.

我正在使用这个:{{product.category_id}} 在 category.twig 页面。

Opencart 3.0.2.0

感谢您的帮助

PS:我进行了很多搜索,但所有参考资料要么是针对产品页面(我有它的地方和它工作的地方),要么只显示我也有的最后一个类别 ID。但我需要为类别页面上的产品分配所有类别 ID。所以像这样的所有链接:Opencart get category_id in category page 都无济于事。

我看到我被否决了,所以让我试着解释一下。 我在 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']
                    );
                }
            }

及以下我有:

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

在category.twig里面我有:

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

但这只是给我一个类别 ID,而不是所有 ID。

已在 opencart 3.0.3.6 中测试,但应该适用于您的版本

文件:catalog/controller/product/category

添加产品之前

$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
)

在 $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
                );

在产品循环中 category.twig 您可以访问所有类别

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

还没有完全测试它,因为我使用的是不同的主题,但它应该可以工作。祝你好运