Kategoriat
Uncategorized

How to display products from subcategory to parent category in opencart? – Stack Overflow

28down voteaccepted

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);
shareedit
2
This is really helpful. simple solution, great impact for user experience. – Yuda Prawira Mar 3 ’15 at 11:38
1
Just to clarify, this modifies the core files of OpenCart, which is rather nasty. Not that working with vQmod is not ugly as hell as well… – Nacho Mar 3 ’15 at 12:59
1
Great Solution. Thank You Sir – amir Jan 19 ’16 at 19:58
1
I agree, worked nicely even in the newer versions (2.0.3.1), the var name just changed to $filter_data, it’s at the line 170. – giovannipds May 16 ’16 at 3:38

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 - jay@myd3.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>

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

shareedit

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!

Lähde: How to display products from subcategory to parent category in opencart? – Stack Overflow