[英]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.