[英]Opencart 2.0 attributes in category.tpl page
我想在 category.tpl 页面上的产品中添加属性。 我只找到了以前 opencart 版本的答案。 但在这个中它不起作用:
- 在 /catalog/model/catalog/product.php中将内容替换为
"public function getProductAttributes($product_id) {",
public function getProductAttributesnocat($product_id) { $product_attribute_data = array(); $product_attribute_query = $this->db->query("SELECT a.attribute_id, ad.name, pa.text FROM " . DB_PREFIX . "product_attribute pa LEFT JOIN " . DB_PREFIX . "attribute a ON (pa.attribute_id = a.attribute_id) LEFT JOIN " . DB_PREFIX . "attribute_description ad ON (a.attribute_id = ad.attribute_id) WHERE pa.product_id = '" . (int)$product_id . "' AND ad.language_id = '" . (int)$this->config->get('config_language_id') . "' AND pa.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY a.sort_order, ad.name"); foreach ($product_attribute_query->rows as $product_attribute) { $product_attribute_data[] = array( 'attribute_id' => $product_attribute['attribute_id'], 'name' => $product_attribute['name'], 'text' => $product_attribute['text'] ); } return $product_attribute_data; }
- 在 /catalog/controller/product/category.php之后
$data['products'][] = array(
添加
'attribute' => $this->model_catalog_product->getProductAttributes($result['product_id']),
- 在 /catalog/view/my_theme/default/template/product/category.tpl之前
<?php if ($product['rating']) { ?>
添加
<?php if ($product['attribute']) { ?> <?php foreach ($product['attribute'] as $attribute) { ?> <span><?php echo $attribute['name']; ?>:</span> <?php echo $attribute['text']; ?><br /> <?php } ?> <?php } ?>
结果 opencart 说:
注意:未定义索引:第 127 行 .../catalog/view/theme/my_theme/template/product/category.tpl 中的属性
但我不擅长 php。 如果您能提供帮助,将不胜感激。
虽然我建议使用 VqMod,但是手动你可以做以下操作: 打开 catalog/controller/product/category.php 并找到以下代码:
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
);
现在在其中添加一行'attribute_groups' => $this->model_catalog_product->getProductAttributes($result['product_id']),那么 products 数组将如下所示:
$data['products'][] = array(
'product_id' => $result['product_id'],
'thumb' => $image,
'attribute_groups' => $this->model_catalog_product->getProductAttributes($result['product_id']),
'name' => $result['name'],
'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
'price' => $price,
'special' => $special,
'tax' => $tax,
'minimum' => $result['minimum'] > 0 ? $result['minimum'] : 1,
'rating' => $result['rating'],
'href' => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
);
现在打开 catalog/view/theme/YOUR_THEME_FOLDER/template/product/category.tpl 并在<p><?php echo $product['description']; ?></p>
下面添加以下代码<p><?php echo $product['description']; ?></p>
<p>
<?php if ($product['attribute_groups']) { ?>
<div class="tab-pane" id="tab-specification">
<table class="table table-bordered">
<?php foreach ($product['attribute_groups'] as $attribute_group) { ?>
<thead>
<tr>
<td colspan="2"><strong><?php echo $attribute_group['name']; ?></strong></td>
</tr>
</thead>
<tbody>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<tr>
<td><?php echo $attribute['name']; ?></td>
<td><?php echo $attribute['text']; ?></td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
</table>
</div>
<?php } ?>
</p>
保存并重新加载它将显示类别产品中的属性
更多详细信息和演示, 请访问https://webocreation.com/blog/show-attributes-of-products-in-category-tpl-page-opencart-2-0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.