0

I have a category collection in Magento 1.9

This is my code :-

    $categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', array('eq' => 4))
    ->addIsActiveFilter()
    ->addAttributeToSort('name', 'ASC');
    foreach ($categories as $cat) {

       $category = Mage::getModel('catalog/category')->load($cat->getId());

       $categories->removeItemByKey($cat->getId);
       echo '<pre>';
       //echo $cat->getIsActive();
       print_r($categories->getData());
       exit;
    }

I need to remove the element corresponding to the current categoryId from the collection in the current loop so that when I use the same collection anywhere after the first loop I do not get the same category again.

But when I do $categories->removeItemByKey($cat->getId) it does not work. It does not remove the corresponding id element from the categories collection.

Am I doing something wrong or does removeItemByKey() not work for category collection and I have to do something else.

I am trying to find this for 5 hours but to no avail.

  • better you can collect needed categories or get collection using neq of respective category which is you want to remove. – Sathish Aug 31 '18 at 10:32
  • Not understand what exactly you want to do . Can you please little describe ? – Ansar Husain Aug 31 '18 at 10:33
1

Try this:

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', array('eq' => 4))
    ->addAttributeToFilter('entity_id', array('neq' => {set you category id here}))
    ->addIsActiveFilter()
    ->addAttributeToSort('name', 'ASC');
echo $categories->getSelect();
foreach ($categories as $cat) {


    echo '<pre>';
    //echo $cat->getIsActive();
    print_r($categories->getData());
    exit;
}

OR you can exclude that by particular category ID

$categories = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', array('eq' => {set you category id here}))
    ->addIsActiveFilter()
    ->addAttributeToSort('name', 'ASC');
echo $categories->getSelect();
foreach ($categories as $cat) {

    if($cat->getId() != 5)
    echo '<pre>';
    //echo $cat->getIsActive();
    print_r($categories->getData());
    exit;
}

Here is the clone feature.

$categories = Mage::getModel('catalog/category');

$categoriesCollection = clone $categories;

$categoriesCollectionFilter = $categoriesCollection
        ->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', array('eq' => 3))
    ->addIsActiveFilter()
    ->addAttributeToSort('name', 'ASC')
->addAttributeToFilter('entity_id', array('neq' => 5));
echo "<pre>";
    print_r($categoriesCollectionFilter->getData());
echo "<br><br><br><br>";
$categoriesOther = $categories->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('parent_id', array('eq' => 3))
    ->addIsActiveFilter()
    ->addAttributeToSort('name', 'ASC');
    print_r($categoriesOther->getData());
exit;
  • 1
    I need to remove the element corresponding to the current categoryId from the collection in the current loop so that when I use the same collection anywhere after the first loop I do not get the same category again. Sorry I did not explain properly. I edited the question to explain this properly. – Sushant Gurjar Aug 31 '18 at 10:37
  • @SushantGurjar, I have updated answer. – kunj Aug 31 '18 at 11:14

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.