我想知道如何在类别页面上显示产品类别ID(给定产品的所有猫ID)。因为现在我只能添加主要的/ top / first。
我使用的是:category.twig页面中的{{product.category_id}}。
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。
答案 0 :(得分:1)
在opencart 3.0.3.6中进行了测试,但应该可以在您的版本中使用
文件:目录/控制器/产品/类别
添加产品之前
$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(块
// 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>
由于我正在使用其他主题,因此尚未对其进行全面测试,但它应该可以工作。祝你好运