【问题标题】:How to get sub-categories of a specific parent category?如何获取特定父类别的子类别?
【发布时间】:2011-03-08 13:08:06
【问题描述】:

我有一个 id = 10 的主类别(父类别)。我只想回显它的子类别。我该怎么做?

【问题讨论】:

    标签: magento


    【解决方案1】:
    $children = Mage::getModel('catalog/category')->getCategories(10);
    foreach ($children as $category) {
        echo $category->getName();
    }
    

    【讨论】:

    • @clockworkgeek 您如何获得子类别的 url?谢谢。
    • @shnozolla 只需使用 getUrl() 而不是 getName()
    • 在这个对象数组中,需要使用getRequestPath()来获取url
    • @CodeCodeO-1-O-log-n getRequestPath() 只获取 URL 的路径部分,而不是整个 URL。它将缺少域、index.php(如果重写不起作用)和 SID(如果需要跳域)部分。
    • 在我的情况下,即使我提供了正确的父类别 ID,我也总是得到根类别。
    【解决方案2】:

    如果您想获取每个当前类别的子类别,此代码可能会有所帮助

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

    【讨论】:

      【解决方案3】:

      另一种方式:

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

      【讨论】:

        【解决方案4】:
        <?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

        【讨论】:

          【解决方案5】:

          这是我的解决方案

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-08-02
              • 2023-03-17
              • 1970-01-01
              • 1970-01-01
              • 2011-07-30
              • 2014-04-22
              相关资源
              最近更新 更多