Search code examples
phpmagentoopen-sourcemagento-1.7

Display product list from same selected subcategory for this product on product detail page


I am struggling from last two days to display product list (only product name with link) at product detail page which have same subcategory as detailed product has. In detail, I have two level category:Please take a look at image:

enter image description here

Now suppose when user goes to prodcut "Zest" item detail page then he can only see "Zest" and "Exotic" items into my list. Not the other items from subcategory (e.g. Indonesian... Blue border color). Here I aam able to get category id of "PODS" (its main category) but cannot get id of "House Blend(2)" subcategory.

Can anyone please help to get these product list? Thanks in advance.


Solution

  • To get all subcategories of a category:

    $children = Mage::getModel('catalog/category')->getCategories(50);
    $searchInCategories = '';
    foreach ($children as $category) {
    
        //Add , after every id
        if (strlen($searchInCategories) > 0)
            $searchInCategories .= ',';
    
        $searchInCategories .= $category->getId();
    }
    

    Where 50 is a category id, in your case this would be PODS id.

    Now you need to alter the search query for your products:

    ->addAttributeToFilter('category_id', array('in' => array('finset' => $searchInCategories)))