Magento – Exclude Parent categories from product collection

categorymagento-1.8

Hi i have below categories,

Category 1 (id 1)
   - Sub category 1 (id 11)
   - Sub Category 2 (id 12)

Category 2 (id 2)
   - Sub Category 3 (id 13)
   - Sub category 4 (id 14)

And all my products are assigned to One Parent and one child category.

If i use

    foreach ($this->getItemCollection()->getItems() as $item) {
        print_r($item->getCategoryIds());
        ...............
}

Below is my result

Array
(
    [0] => 1
    [1] => 11
)
Array
(
    [0] => 1
    [1] => 12
)
Array
(
    [0] => 2
    [1] => 13
)
Array
(
    [0] => 2
    [1] => 14
)

Now how can i exclude product's parent category id's, this case how to remove categories having id's 1 and 2

Best Answer

You can try this way. First find number of categories ie

$item_length = count($item->getCategoryIds());

Next, instead of using foreach loop for categoryIds use for loop

This should look like

for($i=1; $i<$item_length; $i++){
     print($item->getCategoryIds[i]);
}

Frankly speaking i haven't tried this code yet. Please try and put your comments below whether it works or not. Meanwhile i'll try it myself too