首页 > 其他分享 >Magento: 获取类别所有子类别 (无限级别-目录树) Get All Sub Categories

Magento: 获取类别所有子类别 (无限级别-目录树) Get All Sub Categories

时间:2023-03-23 12:02:37浏览次数:45  
标签:category getModel Sub Get get Mage 类别 array categories


生成分类目录树(Category Tree)

$rootcatId  = Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
 
function  get_categories($categories)
{
    $array = '<ul>';
     
    foreach ($categories as $category) {
        $cat    = Mage::getModel('catalog/category')->load($category->getId());
        $count  = $cat->getProductCount();
         
        $array .= '<li>'
                  . '<a href="' . Mage::getModel('catalog/category')->load($category->getId())->getUrl(). '">'
                  . $category->getName()
                  . "(".$count.")</a>";
                   
        if ($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')
                              ->getCategories($category->getId());
            $array  .=  get_categories($children);
        }
        $array .= '</li>';
    }
     
    return  $array . '</ul>';
}
 
echo  get_categories($categories);

 

我个人的用法:

function  get_categories($categories,$level=0)
{
    $array = "\n".'<ul class="'.(!$level ? 'nav navbar-nav' :'dropdown-menu dropdown-menu-level-'.$level).'">';
     
    foreach ($categories as $category) 
    {
        // $cat    = Mage::getModel('catalog/category')->load($category->getId());
        // $count  = $cat->getProductCount();
         
        // $array .= "\n".'<li'.($category->hasChildren()?' class="dropdown-submenu"':'').'>'. '<a href="' . Mage::getUrl($category->getUrlPath()). '" class="dropdown-toggle" data-toggle="dropdown">'. $category->getName(). "(".$count.")</a>";
        
        $array .= "\n".'<li'.($category->hasChildren()?' class="dropdown-submenu dropdown-submenu-level-'.$level.'"':'').'>';
		$array .=  '	<a href="' . Mage::getModel('catalog/category')->load($category->getId())->getUrl(). '" class="dropdown-toggle" data-toggle="dropdown">';
		$array .=  $category->getName().($category->hasChildren()?'<span class="glyphicon glyphicon-play glyphicon-level-'.$category->getLevel().'" aria-hidden="true"></span>':'');
		$array .=  '	</a>';
                   
        if ($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId()); 
            $array  .=  get_categories($children,$category->getLevel());
        }
        
        $array .= '</li>'."\n";
    }
     
    return  $array . '</ul>'."\n";
}

 

打印所有类别的方法如下:

//获取所有激活状态的分类模型集合
$categories = Mage::getModel( 'catalog/category' )->getCollection()
		->addAttributeToSelect( '*' )
     	// then the magic happens here:
	 	//->addAttributeToFilter('level', array('eq'=>2))
     	->addIsActiveFilter();
  
//循环Collection,并填充数组,可以根据需要的数据进行填充,本例以分类ID作为key,对应的值作为value
$allCategory = array ();
foreach ( $categories as $category ) {
	 $allCategory [ $category->getParentId()][$category->getId()] = $category ->getName().'('.$category->getLevel().')';
}
  
echo '==><pre>'; print_r($allCategory); echo '</pre>';

 

获取当前类别的子类别 get subcategories of current / parent category in magento

Method 1:
if you want to get subcategories of current category:

$_currentCategoryId = Mage::registry('current_category')->getId();
$childcategories= Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect("*")->addFieldToFilter('parent_id', $_currentCategoryId)->addAttributeToSort('name', 'ASC');
foreach($childCategories as $childCategory)
   echo $childCategory->getName();
endforeach;

 

 

Method 2:
if you want to get subcategories of specific category:

$parentCategoryId = 3;
$children = Mage::getModel('catalog/category')->getCategories($parentCategoryId);
foreach ($children as $category) {
    echo $category->getName();
}

 

 


标签:category,getModel,Sub,Get,get,Mage,类别,array,categories
From: https://blog.51cto.com/u_8895844/6144615

相关文章

  • Magento: 获取产品评论 get all reviews with review summary
    1.根据产品id获取该产品评论$productId=1234;$product=Mage::getModel('catalog/product')->load($productId);$storeId=Mage::app()->getStore()->getId();Mage......
  • SVN - Get remote repository URL
    $svninfofoo.cPath:foo.cName:foo.cURL:http://svn.red-bean.com/repos/test/foo.cRepositoryRoot:http://svn.red-bean.com/repos/testRepository......
  • .NET & Nsubstitute 模拟Http请求
    我们的代码中有时候会需要调用其他平台的接口,在做单元测试的时候,我们不需要测试这些第三方接口是否生效,接口是否有问题,但是如果我们直接调用第三方接口,这些接口的错误又会影......
  • Fetch 基本操作 Get Post Delete Put
    //删除请求asyncfunctionDeleteModel(model:Customer){leturl=`http://localhost:57679/api/Customers/${model.id}`awaitfetch(url,{method:'dele......
  • Qt QtWidget使用Material风格的组件库
    一、qt-material-widget组件库介绍该组件库拥有炫酷的Material风格的组件,并且该组件库基于QtWidget开发的,目前实现了大约20个Material风格的组件,下面教大家如何编译该组件......
  • Nexus 为 Visual Studio 提供 Nuget 代理
    现在绝大多数企业,为了保护公司利益,都会在内网进行代码开发,内网肯定是无法上网的。对于.net来说,如果能够使内网的机器,通过Nuget下载到外网的第三方类库,是一件很重要的事......
  • Sublime Text 备忘清单_开发速查表分享
    SublimeText备忘清单这个SublimeText快速参考速查备忘单显示了它的键盘快捷键和命令。为开发人员分享快速参考备忘单。开发速查表大纲快捷键文本编辑初学者......
  • re.sub()用法的详细介绍
    一、前言在字符串数据处理的过程中,正则表达式是我们经常使用到的,python中使用的则是re模块。下面会通过实际案例介绍re.sub()的详细用法,该函数主要用于替换字符串中的匹......
  • curl [Get]
    项目中经常会有测试线上或者测试环境非本地的接口的数据结构或者返回信息是什么,提前规划字段或者结构,那如何实现呢?这里仅使用get方法获取示例如下:curl-A'Mozilla/5.0......
  • GET 请求与POST 请求
    GET请求携带数据<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title></head><body><!--<formid="l......