With the code below I try to get the children categories from a specific parent category with ID 7.
<?php
$rootCatId = Mage::app()->getStore()->getRootCategoryId();
function getTreeCategories($parentId, $isChild){
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' => $parentId));
$class = ($isChild) ? "sub-cat-list" : "cat-list";
$html .= '<ul class="'.$class.'">';
foreach($allCats as $category)
{
$html .= '<li>'.$category->getName()."";
$subcats = $category->getChildren();
if($subcats != ''){
$html .= getTreeCategories($category->getId(), true);
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
$catlistHtml = getTreeCategories($rootCatId, false);
echo $catlistHtml;
?>
With this code all categories shown. How I can get this tree only from the specific category with ID 7?
Michael
Try this.I hope that you get result.
$children = Mage::getModel('catalog/category')->getCategories(7);
foreach ($children as $category) {
echo $category->getName();
}
More help is here
Related
enter code hereDoes any one can advice me how to remove the icon for main category havent's sub-categories.
I have pass to show sub-categories under main category.
Here is my code:
[enter image description here][1]
My view:
[blade View][2]
[1]: https://i.stack.imgur.com/MbDuB.png
[2]: https://i.stack.imgur.com/ieAqL.png
My Update with code
public static function showCategories($categories, $parent_id = 0)
{
$class ='';
$html = ""; //init
$flag = false;
foreach ($categories as $row) {
if ($row['parent_id'] == $parent_id) {
//we have a category
$link =self::linkCategory($row['category_id'],$row['name']);
$html .= '<ul id="accordion" class="accordion">
<li>
<div class="link accordion-title">'.$row['name'].'<i class="fa fa-chevron-down"></i></div>';
$html .= "<ul class='submenu'>";
foreach ($categories as $subcat) {
$link =self::linkCategory($subcat['category_id'],$subcat['name']);
if ($subcat['parent_id'] ==$row['category_id']){
//we have a subcategory
$html .= '<li>'.$subcat['name'].'</li>';
}
}
$html .= "</ul>";
$html .= "</li>";
$html .= "</ul>";
}
}
return $html;
//call it blade
$menu = URL::showCategories($itemCategory);
first of all its a bad practice that you use HTML in control, you have to use HTML only in blade sec you can do that by making the relation between categories and itself to get the sub cat to every cat then if your blade while you are looping it u can easily use Count() to get number of sub cat if it bigger than 0
add icon if don't add it
#if(Count($cat->sub) > 0)
<i class="icon-class">
#endif
I made a small script that loops all categories a product belongs to on a product page. Like this:
My issue is that all categories are shown in one big list while they all come from a certain amount of top categories. For example for the image shown, all categories come from allergenen intoleranties and voedingsstijlen. The customer doesn't see this.
I would like to split the list and show the top categorie the sub categories belong to like this:
So I guess I first need to get all parentids from the categories then with those ids I need to get all the top categorie names, loop them, and inside that loop I loop the subcategories.
How would I do that in magento structured code?
The first image is shown with the following code:
<?php
// Haal alle categorieen op waar het product onder valt
$currentCatIds = $_product->getCategoryIds();
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addAttributeToSelect('name')
->addAttributeToSelect('url')
->addAttributeToFilter('entity_id', $currentCatIds)
->addIsActiveFilter();
$sorted = sort($categoryCollection);
$catlist .= '<ul class="categorielijst">';
foreach ($categoryCollection as $cat) {
if ($cat->getName() != 'Root') {
$catlist .= '
<li>' . $cat->getName() . '</li>';
}
}
$catlist .= '</ul>';
echo $catlist;
?>
$categoryCollection = Mage::getResourceModel('catalog/category_collection')
->addFieldToFilter('level',2)
->addAttributeToSelect('name')
->addAttributeToSelect('url');
$out = "<ul>";
foreach($categoryCollection as $cat){
$children = $cat->getChildren();
$out .= "<li>";
$out .= "<a href='".$cat->getUrl()."'>".$cat->getName()."</a>";
$out .="<ul class='sub'>";
foreach($children as $child){
$ChildModel = Mage::getModel('catalog/category')->load($child);
$out .="<li><a href='".$ChildModel->getUrl()."'>".$ChildModel->getName()."</a></li>";
}
$out .="</ul>";
$out .= "</li>";
}
$out .= "</ul>";
echo $out;
I need to display my category tree as a list in a responsive menu.
The idea is to display the highest level categories. and create dynamically a list that will be displayed for each category that has children.
I stumbled upon a code that helped me a bit, but i can't figure how to get the job done.
Here is the code:
<?php
$rootCatId = Mage::app()->getStore()->getRootCategoryId();
function getTreeCategories($parentId, $isChild){
$allCats = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' => $parentId));
$class = ($isChild) ? "sub-cat-list" : "cat-list";
$html .= '<ul class="'.$class.'">';
$children = Mage::getModel('catalog/category')->getCategories(7);
foreach ($children as $category) {
{
$html .= '<li>'.$category->getName()."";
$subcats = $category->getChildren();
if($subcats != ''){
$html .= getTreeCategories($category->getId(), true);
}
$html .= '</li>';
}
$html .= '</ul>';
return $html;
}
$catlistHtml = getTreeCategories($rootCatId, false);
echo $catlistHtml;
?>
Thank you in advance.
you can use this to create category tree:
<?php
$rootCatId= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootCatId);
$output= '<ul>';
foreach($categories as $category) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$output .= '<li>' . '' . $category->getName() . "";
if ($category->hasChildren()) {
$children = Mage::getModel('catalog/category')->getCategories($category->getId());
$array .= get_categories($children);
}
$output .= '</li>';
}
echo $output . '</ul>';
?>
For the effects of showing / hiding categories and subcategories, you can use plain css or jQuery / Prototype.
Here are my costume functions that I wrote for the menu and submenu in my wordpress theme, but after I tested it the submenu disappeared when I clicked on a submenu, because wordpress doesnt separate categories from subcategories, so the parameter for them is "cat", which means that when I click on a submenu then the function that creates the submenu checks if cat=id in the url has child categories but it doesnt becasue it is a child category, I am new into wordpress and I dont know how to deal with it:
function costume_menu() {
$categories = get_categories('hide_empty=0&style=none&parent=0');
foreach ($categories as $category) {
(is_category($category->term_id)) ? $active = 'class="active_menu"' : $active = '';
$nav = '<li>';
$nav .= '<a '.$active.'href="'.get_category_link($category->term_id).'">'.strtoupper($category->cat_name).'</a>';
$nav .= '</li>';
echo $nav;
}
}
function costume_submenu($cat) {
$categories = get_categories("child_of=$cat&hide_empty=0");
foreach ($categories as $category) {
(is_category($category->term_id)) ? $active = 'class="active_menu"' : $active = '';
$nav = '<li>';
$nav .= '<a '.$active.'href="'.get_category_link($category->term_id).'">'.strtoupper($category->cat_name).'</a>';
$nav .= '</li>';
echo $nav;
}
}
Try the function like this:
function costume_submenu($cat) {
$current_cat=get_category($cat);
if($current_cat->parent==0)
$parent_cat=$cat;
else
$parent_cat=$current_cat->parent;
$categories = get_categories("child_of=$parent_cat&hide_empty=0");
foreach ($categories as $category) {
(is_category($category->term_id)) ? $active = 'class="active_menu"' : $active = '';
$nav = '<li>';
$nav .= '<a '.$active.'href="'.get_category_link($category->term_id).'">'.strtoupper($category->cat_name).'</a>';
$nav .= '</li>';
echo $nav;
}
}
I have a single category, that has 2 subcategories. Within each of these categories, are 5 subcategories.
Is there a way to get a list of all of these 10 sub-sub-categories?
Thanks
EDIT:
Something like this:
Main Category
Sub_Cat_1
Cat_1
Cat_2
Cat_3
Sub_Cat_2
Cat_4
Cat_5
Cat_6
Wanting output like:
Cat_1
Cat_2
Cat_3
Cat_4
Cat_5
Cat_6
Thanks
All answers so far load children categories in a loop which is generally bad practice and causes execution of many SQL queries where a single one would suffice.
Performant Single Query Solution:
Let $parentCategory be your Main Category, then this collection will load all subcategories, two levels below:
$subcategoryCollection = Mage::getModel('catalog/category')
->getCollection()
->addFieldToFilter('level', $parentCategory->getLevel() + 2)
->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '/%']);
The path field contains the category id prefixed with all ancestor ids in the form 1/2/3. Database wise it is a column in catalog_category_entity that has an index, so comparison like this has no performance issues.
Figured it out:
$cat = Mage::getModel('catalog/category')->load(24);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'</li>';
}
}
}
}
Thanks for looking!
I have developed a recursive function to get all children of a category
$categoryObject = Mage::getModel('catalog/category')->load(CATID);
$children = MODEL_OBJECT->getChildCategories($categoryObject);
// IN MODLE FILE
public $_catIds = array();
public function getChildCategories($categoryObject){
$categories = $categoryObject->getChildrenCategories();
foreach ($categories as $catgory){
if($catgory->hasChildren()){
$this->getChildCategories($catgory);
}
$this->_catIds[] = $catgory->getId();
}
return $this->_catIds;
}
Hope this will help you.
$this->getCurrentCategory()->getParentCategory()->getData();
try this
What I do is recursively find the id of the ancestor category.
Once found it, I can print the current category sub categories.
If the current category has no children, than I will just print the father's subcategory.
In this way you can have infinite subcategories and still being able to display a sub category list.
$_helper = Mage::helper("catalog/category");
$rootCat = Mage::app()->getStore()->getRootCategoryId();
$current = Mage::registry('current_category');
show_cat($current,$_helper,$rootCat);
function show_cat($child,$_helper,$rootCat){
if ($child){
//look for anchestor
$parentid = $child->getParentId();
$parent = Mage::getModel("catalog/category")->load($parentid);
if($parentid != $rootCat)
{
//find the anchestor
show_cat($parent,$_helper,$rootCat);
}else{
//is root
$_subcategories = $parent->getChildrenCategories();
if(count($_subcategories)>0){
echo '<ul>';
foreach($_subcategories as $_category){
echo '<li>';
echo ''.$_category->getName().'';
if($child->getId() == $_category->getId()){
$current = Mage::registry('current_category');
if ($current){
//handle current
$_current_subcategories = $current->getChildrenCategories();
if(count($_current_subcategories)>0){
//the current cat has childrens
echo '<ul>';
foreach($_current_subcategories as $_sub_category){
echo '<li>';
echo ''.$_sub_category->getName().'';
echo '</li>';
}
echo '</ul>';
}else{
//the current cat has no childrens
$current_parent = $current->getParentId();
$current_parent = Mage::getModel("catalog/category")->load($current_parent );
$_current_subcategories = $current_parent ->getChildrenCategories();
echo '<ul>';
foreach($_current_subcategories as $_sub_category){
echo '<li>';
echo ''.$_sub_category->getName().'';
echo '</li>';
}
echo '</ul>';
}
}
}
echo '</li>';
}
echo '</ul>';
}
}
}
}