¿Cómo obtener subcategorías de una categoría padre específica?


Tenía una categoría principal (categoría padre) cuyo id = 10. Quiero hacerme eco de sus subcategorías. ¿Cómo puedo hacer eso?

 25
Author: Adrian Toman, 2011-03-08

6 answers

$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
    echo $category->getName();
}
 65
Author: clockworkgeek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-03-08 13:53:06

Este código puede ayudar si desea obtener una categoría secundaria de cada categoría actual

    <?php 
    $layer = Mage::getSingleton('catalog/layer');
    $_category = $layer->getCurrentCategory();
    $currentCategoryId= $_category->getId();
    $children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
    foreach ($children as $category)
    {
          echo $category->getName(); // will return category name 
          echo $category->getRequestPath(); // will return category URL
    }
    ?>
 7
Author: Abhijeet kumar sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-28 07:46:51

Otra manera:

$children = Mage::getModel('catalog/category')->load(10)->getChildrenCategories();
foreach ($children as $category):
    $category = Mage::getModel('catalog/category')->load($category->getId());
    echo '<li><a href="' . $category->getUrl() . '">' . $category->getName() . '</a></li>';
endforeach;
 6
Author: joseantgv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-06 09:08:35
<?php 
$parentCategoryId = 107;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();

// Get 1 Level sub category of Parent category
foreach(explode(',',$subcats) as $subCatid)
              {
                $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<ul><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a>';
   echo '</ul>';
  }
}

?>

Si desea subcategorías de nivel 2, Nivel 3, Nivel 4 o Nivel n de la categoría padre que Haga clic aquí

 2
Author: user3146094,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-11 12:57:03

Esta es mi solución

<?
    // Project  : 
    // File     :
    // Author   : Hidayet Ok ( [email protected] )

    require_once('./app/Mage.php');
    Mage::app("default");

    $rootcatId  = Mage::app()->getStore()->getRootCategoryId();
    $root_cat   = Mage::getModel('catalog/category')->load($rootcatId);

    $categories = get_child_categories($root_cat);

    echo "<pre>";
    print_r($categories);
    echo "</pre>";


    function get_child_categories($parent) {

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

        $categories = $cat_model->load($parent->getId())->getChildrenCategories();

        $ret_arr = array();
        foreach ($categories as $cat) 
        {
            $ret_arr[] = array(
                                'cat_id' => $cat->getId(),
                                'cat_name' => $cat->getName(),
                                'cat_url' => $cat->getUrl(),
                                'child_cats' => get_child_categories($cat),
                                );
        } // foreach sonu

        return $ret_arr;

    } // function sonu ##############


?>

Y la salida es;

Array
(
    [0] => Array
        (
            [cat_id] => 4
            [cat_name] => Women
            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/women.html
            [child_cats] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 10
                            [cat_name] => New Arrivals
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/women/new-arrivals.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 11
                            [cat_name] => Tops & Blouses
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/women/tops-blouses.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [cat_id] => 12
                            [cat_name] => Pants & Denim
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/women/pants-denim.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [cat_id] => 13
                            [cat_name] => Dresses & Skirts
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/women/dresses-skirts.html
                            [child_cats] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [cat_id] => 5
            [cat_name] => Men
            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/men.html
            [child_cats] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 14
                            [cat_name] => New Arrivals
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/men/new-arrivals.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 15
                            [cat_name] => Shirts
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/men/shirts.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [cat_id] => 16
                            [cat_name] => Tees, Knits and Polos
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/men/tees-knits-and-polos.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [cat_id] => 17
                            [cat_name] => Pants & Denim
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/men/pants-denim.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [4] => Array
                        (
                            [cat_id] => 40
                            [cat_name] => Blazers
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/men/blazers.html
                            [child_cats] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [cat_id] => 6
            [cat_name] => Accessories
            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/accessories.html
            [child_cats] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 18
                            [cat_name] => Eyewear
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/accessories/eyewear.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 19
                            [cat_name] => Jewelry
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/accessories/jewelry.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [cat_id] => 20
                            [cat_name] => Shoes
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/accessories/shoes.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [cat_id] => 21
                            [cat_name] => Bags & Luggage
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/accessories/bags-luggage.html
                            [child_cats] => Array
                                (
                                )

                        )

                )

        )

    [3] => Array
        (
            [cat_id] => 7
            [cat_name] => Home & Decor
            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/home-decor.html
            [child_cats] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 22
                            [cat_name] => Books & Music
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/home-decor/books-music.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 23
                            [cat_name] => Bed & Bath
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/home-decor/bed-bath.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [cat_id] => 24
                            [cat_name] => Electronics
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/home-decor/electronics.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [cat_id] => 25
                            [cat_name] => Decorative Accents
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/home-decor/decorative-accents.html
                            [child_cats] => Array
                                (
                                )

                        )

                )

        )

    [4] => Array
        (
            [cat_id] => 8
            [cat_name] => Sale
            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/sale.html
            [child_cats] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 26
                            [cat_name] => Women
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/sale/women.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [cat_id] => 27
                            [cat_name] => Men
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/sale/men.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [cat_id] => 28
                            [cat_name] => Accessories
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/sale/accessories.html
                            [child_cats] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [cat_id] => 29
                            [cat_name] => Home & Decor
                            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/sale/home-decor.html
                            [child_cats] => Array
                                (
                                )

                        )

                )

        )

    [5] => Array
        (
            [cat_id] => 9
            [cat_name] => VIP
            [cat_url] => http://XXXXXXXXXXXXX.grinet.com.tr/kategori/vip.html
            [child_cats] => Array
                (
                )

        )

)
 1
Author: Hidayet Ok,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-31 21:05:06

Para obtener todas las subcategorías active así como inactive

$parent_category_id = 10;

$child_categories = Mage::getResourceModel('catalog/category_collection')
                ->addAttributeToSelect('*')
               ->addAttributeToFilter('is_active', array('in' => array(0,1)))
              ->addAttributeToFilter('parent_id', $parent_category_id);

foreach ($child_categories as $cat)
{
    $sub_category_id = $cat->getId();
    $sub_category_name = $cat->getName();
}
 0
Author: Rahul Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-07 01:21:22