获取儿童类别magento

Ton*_*kie 11 php magento

试图获得活跃的特定类别的孩子.请帮忙.我无法做到这一点.我现在能够向他们展示所有但不具体.非常感谢任何帮助.

$category = Mage::getModel('catalog/category')->load(2);
$category->getChildCategories();
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
Run Code Online (Sandbox Code Playgroud)

liy*_*kat 36

这是加载活动类别的代码

/* Load category by id*/
$cat = Mage::getModel('catalog/category')->load($id);


/*Returns comma separated ids*/
$subcats = $cat->getChildren();

//Print out categories string
#print_r($subcats);

foreach(explode(',',$subcats) as $subCatid)
{
  $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive())
  {
    $caturl     = $_category->getURL();
    $catname     = $_category->getName();
    if($_category->getImageUrl())
    {
      $catimg     = $_category->getImageUrl();
    }
    echo '<h2><a href="'.$caturl.'" title="View the products for this category"><img src="'.$catimg.'" alt="" />'.$catname.'</a></h2>';
  }
}
?>
Run Code Online (Sandbox Code Playgroud)

希望这对你有所帮助.


And*_*ler 13

正如mhaupt所提到的,加载集合而不是循环中的每个类别更快.但是,就我而言,没有必要手动加载子类别.基本上这就是$category->getChildrenCategories()已有的.

还有一个过滤器只能获取活动类别.只需打电话addIsActiveFilter()给集合.

a.)通过加载活动子类别 getChildren()

// 1. Get a list of all child category ids (e.g "12,23,11,42")
$subcategoryIds = $category->getChildren();

// 2. Create collection
$categoryCollection = Mage::getModel('catalog/category')->getCollection();

// 3. Add all attributes to select, otherwise you can not 
//    access things like $cat->getName() etc.
$categoryCollection->addAttributeToSelect('*');

// 4. Filter by ids
$categoryCollection->addIdFilter($subcategoryIds);

// 5. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();
Run Code Online (Sandbox Code Playgroud)

b.)使用加载活动子类别 getChildrenCategories()

// 1. Load collection
$categoryCollection= $category->getChildrenCategories();

// 2. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();
Run Code Online (Sandbox Code Playgroud)

一旦访问该集合,就会从数据库中加载该集合.如果未加载集合并且$subcategories->count()仅调用该集合,则将针对数据库触发"SELECT count(*)"(与此相反count($subcategories),将强制集合加载自身).

迭代集合

foreach($categoryCollection as $category) {
    echo $category->getName();
}
Run Code Online (Sandbox Code Playgroud)

如果在访问集合后向集合添加更多过滤器,则集合将不会自动再次加载.要将更改应用于集合,只需调用$categoryCollection->load()从数据库重新加载集合.


oas*_*ing 5

那些说使用getAllChildren()而不是getChildren()的人完全错了.两种方法都返回完全相同的东西,但有一点不同,getAllChildren(true)将返回一个数组而不是逗号分隔的字符串.getAllChildren($ bool asArray)默认为false.我的观点是,无论哪种方式,你都必须使用

Mage::getModel('catalog/category')->load($catId);
Run Code Online (Sandbox Code Playgroud)

除非你使用下面的函数,否则在循环内部.

private function fetchCatsById($onlyThese)
{
    $cats = Mage::getModel('catalog/category')
                ->getCollection(true)
                ->addAttributeToSelect('*')
                ->addIdFilter($onlyThese)
                ->addAttributeToFilter('level','2')
                ->addIsActiveFilter();

    return $cats;
}

$cats = $this->fetchCatsById($onlyThese);
Run Code Online (Sandbox Code Playgroud)