Aaviya
Aaviya

Reputation: 1190

How to display products from subcategory to parent category in opencart?

I'm trying to find a solution but still no luck. I want to display all products from subcategories to parent category.

I'm using Opencart 1.5.3.1

Upvotes: 10

Views: 25125

Answers (6)

slva2000
slva2000

Reputation: 149

For Opencart 3.x: ./catalog/controller/product/category.php

find:

'filter_category_id' => $category_id,

add after:

'filter_sub_category' => true,

After change file, go to "Modification" and "Clear" and "Update" button click

Upvotes: 0

Ignacio
Ignacio

Reputation: 8035

After reading the source, I figured it out:

In catalog/controller/product/category.php (or wherever you're calling function model_catalog_product->getProducts) you have to add filter_sub_category = true:

$data = array(
    'filter_category_id' => $top_category,
    'filter_sub_category' => true,
    'sort'               => $sort,
    'order'              => $order,
    'start'              => ($page - 1) * $limit,
    'limit'              => $limit
);

$product_total = $this->model_catalog_product->getTotalProducts($data);

Make sure you check the other answers if you're using a later version ;)

Upvotes: 34

giovannipds
giovannipds

Reputation: 3459

Just contributing with one more information, newest versions uses "ocmod", OpenCart's own vqmod feature. It can work the same way through there.

Upvotes: 1

Octavian Ionel
Octavian Ionel

Reputation: 423

catalog/controller/product/category.php

For Opencart Version 2.1.0.2, the solution of Ignacio works fines as well just that:

$data (of version 1.5.x) is now called

$filter_data (line #169)

Then just add the line

'filter_sub_category' => true,

after line #170 ('filter_category_id' => $category_id,)

Thanks Ignacio!

Upvotes: 4

user3204808
user3204808

Reputation: 11

the vqmod method is the best and works easily. just add that file and anything.xml and place it in vqmod > xml

Upvotes: 1

jaywilliams
jaywilliams

Reputation: 311

Another solution, rather than modifying the core files directly, is to use vQmod to modify the file for you. That way when you upgrade to a new version, you won't have to re-install any custom modifications you've made.

Below is an the code you'd use to accomplish this in vQmod:

<?xml version="1.0" encoding="UTF-8"?>
<modification>
    <id>Display products in sub-categories while browsing a parent category</id>
    <version>1.0.0</version>
    <vqmver>2.4.0</vqmver>
    <author>Jay Williams - [email protected]</author>
    <file name="catalog/controller/product/category.php">
        <operation>
            <search position="after"><![CDATA['filter_category_id' => $category_id,]]></search>
            <add><![CDATA['filter_sub_category' => true,]]></add>
        </operation>
    </file>
</modification>

Source: https://gist.github.com/jaywilliams/8044763

Upvotes: 7

Related Questions