如何获取特定父类别的子类别?

时间:2011-03-08 13:08:07

标签: magento

我有一个主要类别(父类别),其id = 10.我想只回显它的子类别。我怎么能这样做?

6 个答案:

答案 0 :(得分:66)

$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
    echo $category->getName();
}

答案 1 :(得分:8)

如果您想获得每个当前类别的子类别

,此代码可能会有所帮助
    <?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
    }
    ?>

答案 2 :(得分:7)

另一种方式:

$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;

答案 3 :(得分:3)

<?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>';
  }
}

?>

如果您想要父级别的2级,3级,4级或n级子类别而不是Click Here

答案 4 :(得分:2)

这是我的解决方案

<?
    // Project  : 
    // File     :
    // Author   : Hidayet Ok ( hidonet@gmail.com )

    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 ##############


?>

,输出为;

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
                (
                )

        )

)

答案 5 :(得分:1)

获取所有active以及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();
}