When working on a client’s Opencart store, I have met the following problem. The products wasn’t appearing in the category page, only in the subcategories. Imagine that you have 5 subcategories in one category. When you open each of them they have products, but the main category stays empty.
I want to display all products from subcategories to parent category.
The problem was even bigger, because I wanted to add filters to the main category, but there were no products to be filtered.
There are 2 ways to fix it:
Option 1: Fix it by editing category.php in controller
Dowload via ftp (File Zilla for example) catalog/controller/product/category.php and open it with a text editor. Then search for model_catalog_product->getProducts. In the $data array you have to add ‘filter_sub_category’ => true.
Your array will look like this:
$data = array( 'filter_category_id' => $category_id, 'filter_sub_category'=> true, 'filter_filter' => $filter, 'sort' => $sort, 'order' => $order, 'start' => ($page - 1) * $limit, 'limit' => $limit );
Better option: By Using vQmod
Rather than modifying the core files directly, you can use vQmod to do this. In that way if you upgrade to new Opencart version, in that way you will keep your custom modifications and won’t need to do them again.
Create a text file and save it as category_parent.xml. Then paste the below code and upload the XML file in public_html/ vqmod/xml/.
Note: You need to have vQmod installed
<?xml version="1.0" encoding="UTF-8"?> <modification> <id>Display products from subcategories while browsing a parent category</id> <version>1.0.0</version> <vqmver>2.4.0</vqmver> <author>Georgi Dyulgerov - ftutorials.com</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>
The solution is tested for Opencart 1.5 to 18.104.22.168. Probably working for 2.0 also.