How to get sub-categories of a specific parent category?
I had a main category (parent 开发者_运维问答category) whose id = 10. I want to echo just its sub-categories. How can I do that?
$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
echo $category->getName();
}
This code may help if you want to get child category of every current category
<?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
}
?>
Another way:
$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;
<?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>';
}
}
?>
If you want 2 level , 3 Level, 4 Level or n Level subcategories of parent category than Click Here
This is my solution
<?
// 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 ##############
?>
and the output is;
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
(
)
)
)
To get all the active
as well as inactive
sub categories
$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();
}
精彩评论