Php 检查类别是否在magento中有子类别
我有分类id。我从这个代码中得到了idPhp 检查类别是否在magento中有子类别,php,magento,magento-1.7,Php,Magento,Magento 1.7,我有分类id。我从这个代码中得到了id <?php echo $current_catid=$this->getCategoryId(); ?> 现在我想检查此类别是否有子类别 如果它有子类别,则会显示子类别图像、名称和url 如果您有当前的\u类别id,则加载类别 $category=Mage::getModel('catalog/category')->加载(id) 并检查count($category->getChildren()) 其他方法用于计数儿童 coun
<?php echo $current_catid=$this->getCategoryId(); ?>
现在我想检查此类别是否有子类别
如果它有子类别,则会显示子类别图像、名称和url 如果您有当前的\u类别id,则加载类别
$category=Mage::getModel('catalog/category')->加载(id)代码>
并检查count($category->getChildren())代码>
其他方法用于计数儿童
count($category->getChildrenNodes());
$category->getChildrenCount();
通过这种方式,您可以检查类别是否有子类别
getChildren()
方法为您提供子类别id,根据id您可以获得类别图像和类别名称。请尝试这一个,在我这里它可以正常工作
<?php
$parentCategoryId = 10;
$categories = Mage::getModel('catalog/category')->load($parentCategoryId)->getChildren();
$catArray = explode(',', $categories);
foreach($catArray as $child)
{
$_child = Mage::getModel( 'catalog/category' )->load( $child );
echo $_child->getName() . '<br />';
echo $_child->getUrl() . '<br />';
echo $_child->getDescription() . '<br />';
}
?>
有点老了,但我正在搜索相同的解决方案,@Mufaddal的解决方案不起作用。然后我找到了getChildrenCategories()
您还有另一个选项来检查类别子类别是否存在
<?php
$currentCategoryId = Mage::registry('current_category')->getId();
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active', 1) //only active categories
->addAttributeToFilter('parent_id', $currentCategoryId);
$currentCat = Mage::registry('current_category');
$subCategories = Mage::getModel('catalog/category')->load($currentCat->getParentId())->getChildrenCategories();
if($collection->getSize() >= 1){//there some thing....}else{
//Get Subcategory....
foreach ($subCategories as $subCategoryId ):
if($subCategoryId->getIsActive())
{ $products = Mage::getModel('catalog/category')->load($subCategoryId->getId())
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4);
<li <?php if($subCategoryId->getId()==$currentCategoryId){?>class="active"<?php } ?>>
<a href="<?php echo $subCategoryId->getURL(); ?>">
<?php //echo $subCategoryId->getName()." (".$products->count().")"; ?>
<?php echo $subCategoryId->getName(); ?>
</a>
</li>
} endforeach;}?>
class=“active”>
}endforeach;}?>
如果它的帮助充分让我知道
谢谢
Ravi实际上取决于是否启用了“使用平面目录类别”选项
因此,检查类别是否有子类别的最佳方法是:
if (Mage::helper('catalog/category_flat')->isEnabled()) {
$childrenCount = $category->getResource()->getChildrenAmount($category);
} else {
$childrenCount = $category->getResource()->getChildrenCount();
}
对于$category,我想您已经有了,如下所示:
$category = Mage::getModel('catalog/category')->load(id);
我尝试了你的代码,但它不起作用。当我数孩子时,它总是显示1,但它有2个孩子类别。我把这个代码$current_catid=$this->getCategoryId()$category=Mage::getModel('catalog/category')->load($current_catid);回显计数($category->getChildren());尝试getChildre()->count()方法,也可以使用count($category->getChildrenNodes());对于count category children,我按照您告诉我的那样尝试了$category->getChildren()->count();这给了我致命的错误
$category = Mage::getModel('catalog/category')->load(id);