php 获取子类别 magento
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18525419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Child categories magento
提问by Tonzkie
Trying to get child of a specific category which is active. Please help. I am having trouble doing it. I'm currently able to show them all but not specifically. Would appreciate any help.
试图获得活跃的特定类别的孩子。请帮忙。我很难做到。我目前能够展示它们,但不能具体展示。将不胜感激任何帮助。
$category = Mage::getModel('catalog/category')->load(2);
$category->getChildCategories();
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
回答by liyakat
here is code to load active category
这是加载活动类别的代码
/* 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>';
}
}
?>
hope this is sure help you.
希望这对你有帮助。
回答by Andreas Riedmüller
As mentioned by mhaupt, it is faster to load a collection rather than each category in a loop. But, as far as I am concerned, there is no need to manually load the child categories. Basically this is what $category->getChildrenCategories()
already does.
正如 mhaupt 所提到的,在循环中加载集合而不是每个类别更快。但是,就我而言,无需手动加载子类别。基本上这就是$category->getChildrenCategories()
已经在做的事情。
There is also a filter to get active categories only. Just call addIsActiveFilter()
on the collection.
还有一个过滤器仅用于获取活动类别。只需调用addIsActiveFilter()
集合。
a.) Load active child categories via getChildren()
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();
b.) Load active child categories with getChildrenCategories()
b.) 加载活动的子类别 getChildrenCategories()
// 1. Load collection
$categoryCollection= $category->getChildrenCategories();
// 2. Add filter to collection to get active categories only
$categoryCollection->addIsActiveFilter();
The collection will be loaded form the database as soon as it is accessed. If the collection is not loaded and $subcategories->count()
is called only a "SELECT count(*)" will be fired against the database (in contrast to count($subcategories)
which will force the collection to load itself).
该集合将在访问后立即从数据库加载。如果集合未加载并且$subcategories->count()
被调用,则只会对数据库触发“SELECT count(*)”(相反,count($subcategories)
这将强制集合加载自身)。
Iterating the collection
迭代集合
foreach($categoryCollection as $category) {
echo $category->getName();
}
If you add more filters to the collection after accessing it, the collection will not load itself again automatically. To apply changes to the collection, just call $categoryCollection->load()
to reload the collection from the database.
如果在访问集合后向集合添加更多过滤器,集合将不会再次自动加载。要将更改应用于集合,只需调用$categoryCollection->load()
从数据库重新加载集合。
回答by oasisfleeting
Those who are saying to use getAllChildren() instead of getChildren() are simply wrong. Both methods return the exact same thing, with one difference, getAllChildren(true) will return an array instead of a comma delimited string. getAllChildren($bool asArray) defaults to false. My point being that either way you're going to have to use
那些说使用 getAllChildren() 而不是 getChildren() 的人完全错了。两种方法都返回完全相同的内容,但有一个区别, getAllChildren(true) 将返回一个数组而不是逗号分隔的字符串。getAllChildren($bool asArray) 默认为 false。我的观点是无论哪种方式你都必须使用
Mage::getModel('catalog/category')->load($catId);
inside of a loop unless you use the function below.
在循环内部,除非您使用下面的函数。
private function fetchCatsById($onlyThese)
{
$cats = Mage::getModel('catalog/category')
->getCollection(true)
->addAttributeToSelect('*')
->addIdFilter($onlyThese)
->addAttributeToFilter('level','2')
->addIsActiveFilter();
return $cats;
}
$cats = $this->fetchCatsById($onlyThese);
回答by mhaupt
The one answer liyakat wrote, should not be used in professional shops, because it raises a performance issue, because of the multiple n time loads of the category object, rather use the collection of categories for that, get all children
liyakat 写的一个答案,不应该在专业商店中使用,因为它引发了性能问题,因为类别对象的多次负载,而是使用类别集合,得到所有孩子
$cat->getAllChildren()
, then limit the category collection by the needed category ids like
,然后通过所需的类别 id 限制类别集合,例如
$coll->addIdFilter($idFilter);
then you won't have to load n times against the database.
那么您将不必针对数据库加载 n 次。
Please do keep in mind that loads within loops are one of the most often used bad code examples in any Magento projects and to avoid them!
请记住,循环内的负载是任何 Magento 项目中最常使用的不良代码示例之一,并避免它们!
回答by MagikVishal
Hello you will see below code
你好你会看到下面的代码
$category_model = Mage::getModel('catalog/category');
$_category = $category_model->load(13);
$all_child_categories = $category_model->getResource()->getAllChildren($_category);
print_r($all_child_categories);
回答by user3146094
If you want any number of subcategories of parent category than Click here http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html
如果您想要任何数量的父类别的子类别,请单击此处http://magentoo.blogspot.com/2014/01/get-all-subcategories-of-parent-category-magento.html