簡體   English   中英

想要在magento中的2級類別名稱

[英]want level 2 category name in magento

 $product = Mage::getModel('catalog/product')->load($item->getProductId());
 $pro = $product->getCategoryName(); //category id is fetched here
 $category = Mage::getModel('catalog/category')->load($pro);
  • 根目錄
    • 家具
    • 電子產品
      • 電腦
        • 處理器
    • 服飾

因此,當我獲得處理器作為類別名稱時,我想顯示其級別2的類別名稱,即電子

得到了解決方案,下面的代碼將獲取當前類別,然后繼續獲取父類別,直到獲得最高類別(而不是根類別)為止。

$product = Mage::getModel('catalog/product')->load($item->getProductId());
                    $pro = $product->getCategoryName(); //category id is fetched here

                    $category = Mage::getModel('catalog/category')->load($pro);

                    if ($category)
                    {
                        while($category->getLevel() != 2)
                        {
                            $category = $category->getParentCategory();
                            if (!$category)
                            {
                                break;
                            }
                        }
                        if ($category)
                        {
                            echo $category->getName();
                        }
                        else
                        {
                            echo 'Cannot find parent category';
                        }
                    }
$CategoryCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('level',2);

你可以用這個

$categories = Mage::getModel('catalog/category')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addIsActiveFilter()
                ->addAttributeToFilter('level',2)

要么

<?php

$_category = Mage::registry('current_category');

$_category = $this->getCurrentCategory();

// Category Name

echo $_category->getName();

// Category Level

echo $_category->getLevel();

?>

喲可以用這樣的東西

public function get_cat_from_product($prod_id,$level){

    $categoryIds = Mage::getModel('catalog/product')->load($prod_id)->getCategoryIds();

    if(count($categoryIds) ){
        $firstCategoryId = $categoryIds[$level-1]; //level 2 -> pos 1
        $_category = Mage::getModel('catalog/category')->load($firstCategoryId);

        echo $_category->getName();
    }else{
        return null;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM