-2

I want to add attributes in products on the category.tpl page. I found answers only for previous opencart versions. But in this one it doesn't work:

- in /catalog/model/catalog/product.php replaced the contents to

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

- in /catalog/controller/product/category.php after

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

added

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

- in /catalog/view/my_theme/default/template/product/category.tpl before

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

added

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

As result opencart said:

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

But I'm not strong in php. Will be very appreciate, if you can help.

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
Loloka
  • 3
  • 3

1 Answers1

0

Although I suggest to use VqMod, but manually you can do following: Open catalog/controller/product/category.php and find following code:

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

Now add one line in it 'attribute_groups' => $this->model_catalog_product->getProductAttributes($result['product_id']), then the products array will be like below:

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

Now open catalog/view/theme/YOUR_THEME_FOLDER/template/product/category.tpl and add following codes just below <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>

Save and reload it will show attributes in category's products

More details and demo at https://webocreation.com/blog/show-attributes-of-products-in-category-tpl-page-opencart-2-0

Rupak Nepali
  • 719
  • 1
  • 6
  • 13
  • I installed vqmod and followed instruction. Alas. Notice: Undefined index: attribute_groups in /home/m/marsha/sun/public_html/vqmod/vqcache/vq2-catalog_view_theme_coloring_template_product_category.tpl on line 112 in line 112: – Loloka Oct 06 '15 at 19:21
  • It means that at catalog/controller/product/category.php you have not added 'attribute_groups' => $this->model_catalog_product->getProductAttributes($result['product_id']), add the above line to product array at category.php – Rupak Nepali Oct 06 '15 at 19:25
  • hmm, i added it at previous time, and tried again, but it doesn't work https://www.evernote.com/shard/s76/sh/deddc369-ee4f-4a11-ab8e-b58e9d551dbb/98bff9caf5da85782f85eb3411126119 – Loloka Oct 07 '15 at 12:56
  • It began to work after cleaning cache modifier. Thanks! – Loloka Oct 21 '15 at 10:01