category.tpl 页面中的 Opencart 2.0 属性

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说:

Notice: Undefined index: attribute in .../catalog/view/theme/my_theme/template/product/category.tpl on line 127

但我不擅长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']), 那么产品数组将如下所示:

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