我正在尝试在列表页面上获取每个产品的最新级别类别名称及其产品数量。解释如下 -
Category
-> Sub-category 1
->Sub-sub-category 11 -> Product 1
->Sub-sub-category 12
-> Sub-sub-sub-category 13 ->Product 2
-> Sub-category 2
->Sub-sub-category 21
-> Sub-sub-sub-category 22 ->Product 3
->Sub-sub-category 23 -> Product 4
我想只获得类别" 子类别11 "," 子子类别13 "," 子子类别22 "和" 子类别23 "这是产品1,产品2,产品3,产品4的最后一类。 我在下面找到了get last level category name of a product in magento链接,但它只获得了第三级别的类别。
答案 0 :(得分:4)
有点晚了,但这可以很简单地完成,当你想获得给定产品最深层次的类别时使用这个功能:
public function getProductLastLevelCategory(Mage_Catalog_Model_Product $product)
{
/** @var Mage_Catalog_Model_Resource_Category_Collection $categories */
$categories = $product->getCategoryCollection()
->addAttributeToSelect('*')
->addOrder('level');
return $categories->getFirstItem();
}
基本集合包含给定产品所属的所有类别,并且技巧依赖于按级别排序(隐式DESC)。
答案 1 :(得分:1)
您好,您可以使用方法getChildrenCount()
作为您的类别,并检查它是否等于0
if($category->getChildrenCount() == 0) {
// Do something
}
答案 2 :(得分:0)
在我的问题上我尝试了这段代码和成功
<?php
$_helper = $this->helper('catalog/category');
$_storeCategories = $_helper->getStoreCategories();
?>
<?php
function getLastCategory($_categories)
{
$_helper = Mage::helper('catalog/category');
if (count($_categories) > 0) {
foreach ($_categories as $_category) {
$_category = Mage::getModel('catalog/category')->load($_category->getId());
$_subcategories = $_category->getChildrenCategories();
if (count($_subcategories) > 0) {
getLastCategory($_subcategories);
} else {
echo($_category->getName()."<br />" );
}
}
}
}
$lastCategory = getLastCategory($_storeCategories);
?>