1

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

  • Not the best idea, have a look on my answer, but to not delete it... what about array_shift($item->getCategoryIds()) – Fabian Blechschmidt Apr 11 '14 at 7:14
  • My question here would be do you need the products attached to the parent categories? – David Manners May 22 '14 at 20:07
1

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

| improve this answer | |
  • How could this will exclude parent category ? – Vinoth Mar 12 '14 at 6:48
  • In your listing the first category you are getting is the parent category. Skipping the first value will naturally skip the first ie parent category – Dexter Mar 12 '14 at 6:50
  • thats right, but it's not for all cases, on some cases products don't have parent category ! – Vinoth Mar 12 '14 at 7:09
  • There is another way to do this then. If you are only concerned about the child category of certain category, you can try using this code $_categories=$this->getCurrentChildCategories(); this will get all the child category of the current category. – Dexter Mar 12 '14 at 8:03
1

You can get the category collection and then filter by being parent

$product->getCategoryCollection()->addAttributeToFilter('children_count', 0);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.