0

Getting all children from specified category then pulling products from all the childrens

$_category = Mage::getModel('catalog/category')->load(5);
$lids = $_category->getAllChildren();
$ids = explode(',',$lids);
echo '<pre>';
print_r($ids);
echo '</pre>';
if ($ids)
{
    foreach ($ids as $id)
    {
        $cat = Mage::getModel('catalog/category');
        $cat->load($id);
        if($cat->getIsActive()==1)
        {
        $category1 = Mage::getModel('catalog/category')->load($cat->getId());
        $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToSelect('megamenu_category_product')
            ->addCategoryFilter($category1);

            foreach ($products as $product) {
                if ($product->getData('megamenu_category_product') == true){
                    echo "<div>" . $product->getData('name') . " - ". $product->getSku() . "</div>";
                    break;
                }else{
                    //echo 'FOO<br/>';
                }
            }
        }
    }
}

because I used '->addAttributeToSelect("megamenu_category_product")' it won't display product->getName() What am I doing wrong ?

2
  • ka.lpe.sh/2014/11/06/… .. doing a google search is more faster than adding a question here. Apr 21, 2017 at 11:45
  • I managed to do it but still ... would you look at the code ?
    – Rob D. A.
    Apr 21, 2017 at 12:37

1 Answer 1

1
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();

$ids = $tree->getCollection()->getAllIds();

if ($ids)
{
     foreach ($ids as $id)
  {
     $cat = Mage::getModel('catalog/category');
     $cat->load($id);
     if($cat->getLevel()==3 && $cat->getIsActive()==1)
     {
        $category1 = Mage::getModel('catalog/category')->load($cat->getId());
        $products = Mage::getResourceModel('catalog/product_collection')
                                ->addAttributeToSelect('name')
                             ->addCategoryFilter($category1);
        echo "<b>".$cat->getName()."</b><br>";
        foreach ($products as $product) { //print_r($product->getData());exit;
                echo " &nbsp; &nbsp; &nbsp; " . $product->getName() . " - ". $product->getSku() . "<br/>";
        }
     }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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