This opencart tutorial shows how to show attributes of products in category page in Opencart 2.0 and Opencart 3.0.3.2 we will provide OCMOD and VqModafter some time:
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(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_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(trim(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'))), 0, $this->config->get('theme_' . $this->config->get('config_theme') . '_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)
);
For OpenCart 3.0+
Now open catalog/view/theme/YOUR_THEME_FOLDER/template/product/category.twig and add following codes just below <p>{{ product.description }}</p>
{% if product.attribute_groups %}
<div class="tab-pane" id="tab-specification">
<table class="table table-bordered">
{% for attribute_group in product.attribute_groups %}
<thead>
<tr>
<td colspan="2"><strong>{{ attribute_group.name }}</strong></td>
</tr>
</thead>
<tbody>
{% for attribute in attribute_group.attribute %}
<tr>
<td>{{ attribute.name }}</td>
<td>{{ attribute.text }}</td>
</tr>
{% endfor %}
</tbody>
{% endfor %}
</table>
</div>
{% endif %}
For OpenCart 2.0+
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 and it will show attributes in category’s products
If you need to do for the featured products, bestseller products then follow similar code changes in their controller and view files.
Comment below or let us know if you have any questions or requirements for OpenCart. You can subscribe to our youtube channel for OpenCart tutorials.
worked on opencart 2.1.0.1. Thank you!
Can I get assistence?
I receive this warning:
Notice: Undefined index: attribute_groups in /web/htdocs/www.cadoreshop.com/home/catalog/view/theme/theme561/template/product/category.tpl on line 139
did you solve this?
This is solved by doing the if() statement check
I receive this warning:
Notice: Undefined index: attribute_groups in /web/htdocs/www.cadoreshop.com/home/catalog/view/theme/theme561/template/product/category.tpl on line 139
Can I get assistence?
Hello rupak do you have this in ocmod to OC 2.0, this change works like a charm if edited manualy the files, but my objetive is create ocmod.I try to create another similar of this but the only change is show ‘sku value’ and i have some issues, in register say
the line of search not found i put this line “‘thumb’ => $image,” Can you help me?
Hi!
I am using Opencast 2.2.0. I did exactly what you’ve said countless times.
I copied the attributes display from product.php and .tpl . No luck.
I get “Undefined index: attribute_groups in /var/www/virtual/…”
I am getting crazy here. What am I missing?
Thank you!
Hi Marius,
You need to change at category.php and category.tpl
I checked it at 2.2 version and it is working fine with above code.
Please try it again 🙂
Hope you will be able to fix it.
Once I got chance I will publish OCMOD
Thanks
Rupak
Hi Rupak
Do you think its possible to display the category of the product?
Hi Rupak
Do you think its possible to display the category of the product? if it is could u help me? thanks
Hi Eds,
Yes it is possible but it will redundancy of category as we are already in category page so hope not needed to show category in category page for products.
If needed I will soon write some code for it
Worked for me. Thank you for posting this tutorial.
How can modify this code to display attributes in this format?
Attribute Name 1 Attribute Name 2 Attribute Name 3
Attribute Value 1 Attribute Value 1 Attribute Value 1
how about product.tpl I want to show right side all attributes on product page how can I do that ? Thanks
Hi Faruk,
It is easy in product page.
You can add following code at catalog\view\theme\***\template\product\product.tpl where ever you want to show the attributes.
I have take code from product.tpl, so you can remove those table tags and implement your design
Thanks
Rupak
Hy,
How can list only first 3 attributes not all
Thanks
Thank you so much for sharing this, there is one small mistype, in Opencart 3 part the category.tpl should be category.twig
Thanks for the heads up. Corrected.
Thanks for the heads up. Corrected.
Hi,
I’m trying to make it work on 3.0.3.1 version.
Added code as described in “For OpenCart 3.0 “, cleaned OC & browser cache and reloaded. No attributes displayed on category page.
Could have I support?
How to show this on homepage in featured product module? Adding this code to featured.php displays error “Undefienied variable”.
OK I have the sollution for featured.tpl. Simply change one part of the code, and it should be: ‘attribute_groups’ => $this->model_catalog_product->getProductAttributes($product_info[‘product_id’]) but does anyone have an idea how to sort artibutes? Change position in admin panel does not help.
Hi Rupak
Came across this great site. Showing attributes on category page is what I have been trying to do for weeks!
Have used your code as directed in OC 3.0.3.6 but get the following error appears on page:
http://localhost/test_website/index.php?route=product/category&path=33
Parse error: syntax error, unexpected ‘=>’ (T_DOUBLE_ARROW) in C:\xampp\htdocs\test_website\catalog\controller\product\category.php on line 208
Can you advise?
Much appreciated as your site has helped much already. Thank you.