如何从Magento获取特定的类别级别,我的类别设置现在如下所示。
root_catalog
|-Shop
|-Shoes
|-T-shirts
|-Brands
|-Nike
|-Womens
|-Mens
|-Adidas
|-Asics
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php echo $category->getName(); ?>
<?php endif ?>当调用$category->getName()时,我想只显示Brand Name,这可能吗?
发布于 2013-04-01 13:42:15
您可以从$category = Mage::getModel('catalog/category')->load( $categories[1]) )->getLevel()获取类别级别,然后检查您的品牌名称类别级别,如果匹配,则显示名称。
例如,假设品牌类别级别为3
<?php if( $category = Mage::getModel('catalog/category')->load( $categories[1]) ): ?>
<?php if($category->getLevel() == 3)
echo $category->getName(); ?>
<?php endif ?>
<?php endif ?>发布于 2014-01-27 17:26:29
ANKIT的答案是好的,但它可以通过实际查询特定的级别来改进,而不是加载整个集合并执行条件。例如,如果您想要获取特定级别的所有类别:
<ul>
<?php $categories = Mage::getModel('catalog/category')
->getCollection()
// magic is prepared here..
->addAttributeToSelect('*')
// then the magic happens here:
->addAttributeToFilter('level', array('eq'=>2))
->load();
foreach($categories as $category):
?>
<li>$category->getName()</li>
<?php endforeach; ?>
</ul>发布于 2018-11-30 18:23:22
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$categories = $product->getCategoryIds(); /*will return category ids array*/
foreach($categories as $category){
$cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
if ($cat->getLevel() == 2) {
$catName = $cat->getName().","; ?>
<div class="brand_bg">
<label><?php /* @escapeNotVerified */ echo __('Category :') ?></label>
<?php echo $catName; ?>
</div>
<?php } ?>
<?php } ?>
?>https://stackoverflow.com/questions/15731475
复制相似问题