category.tpl页面中的Opencart 2.0属性

时间:2015-10-05 12:02:58

标签: php opencart opencart2.x

我想在category.tpl页面上的产品中添加属性。 我只找到了以前的opencart版本的答案。但是在这个中它不起作用:

- 位于/catalog/model/catalog/product.php 将内容替换为

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;
}

$data['products'][] = array(

- 位于/catalog/controller/product/category.php 之后

 'attribute' => $this->model_catalog_product->getProductAttributes($result['product_id']),

添加了

<?php if ($product['rating']) { ?>

- 位于/catalog/view/my_theme/default/template/product/category.tpl

<?php if ($product['attribute']) { ?>
     <?php foreach ($product['attribute'] as $attribute) { ?>
        <span><?php echo $attribute['name']; ?>:</span> <?php echo $attribute['text']; ?><br />   
    <?php } ?>
<?php } ?>

加入

function xmlToJson(xml) {
    // Create the return object
    var obj = {};
    if (xml.nodeKind() == "element") {
        if (xml.attributes().length() > 0) {
            for (var j = 0; j < xml.attributes().length(); j++) {
                var attributeName = xml.attributes()[j].name();
                obj[attributeName] = String(xml.attributes()[j]);
            }
        }
    } else if (xml.nodeKind() == "text") {
        obj['text'] = xml.text();
    }
    if (xml.children()) {
        for (var i = 0; i < xml.children().length(); i++) {
            var item = xml.child(i);
            if (xml.children()[i].nodeKind() == "text") {
                obj['text'] = xml.children()[i].toString();
            } else {
                var nodeName = item.name();
                if (typeof(obj[nodeName]) == "undefined") {
                    obj[nodeName] = xmlToJson(item);
                } else {
                    if (typeof(obj[nodeName].push) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                    }
                    obj[nodeName].push(xmlToJson(item));
                }
            }
        }
    }
    return obj;
};

结果opencart说:

  

注意:未定义的索引:第127行的... / catalog / view / theme / my_theme / template / product / category.tpl中的属性

但我在php中并不强大。如果你能提供帮助,我们将非常感激。

1 个答案:

答案 0 :(得分:0)

虽然我建议使用VqMod,但您可以手动执行以下操作: 打开目录/ 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'=&gt; $ this-&gt; model_catalog_product-&gt; 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)
            );

现在打开目录/视图/主题/ 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