php 在子类别页面中获取父类别 ID - Magento

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11707667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:55:18  来源:igfitidea点击:

Get Parent Category Id in Sub Category Page - Magento

phpmagento

提问by Abhi

I need a small line of code. I have a category like

我需要一小行代码。我有一个类别

  • Cat-1
    • Sub-Cat-1
    • Sub-Cat-2
  • Cat-2
    • Sub-Cat-3
    • Sub-Cat-4
  • Cat-1
    • 子类 1
    • 子类 2
  • 2 类
    • 子类 3
    • 子类 4

In Sub-Cats page i need to get Root category's Id. Like In "Sub-Cat-1" i need to get "Cat-1's" ID. You can say in children category page, i need parent category's Id. act i am using short url like "abc.com/Sub-Cat-1", nither index.php nor root category in URL. I am using Magento-1.4.1. Please help me. Thanks in advance.

在 Sub-Cats 页面中,我需要获取 Root 类别的 Id。就像在“Sub-Cat-1”中一样,我需要获得“Cat-1”的 ID。您可以在儿童类别页面中说,我需要父类别的 ID。我正在使用像“abc.com/Sub-Cat-1”这样的短网址,既没有 index.php 也没有 URL 中的根类别。我正在使用 Magento-1.4.1。请帮我。提前致谢。

回答by Abhi

I got the solution.

我得到了解决方案。

echo $cat_idd = $this->getCurrentCategory()->getParentCategory()->getId(); 

回答by MagePsycho

Try to use as:

尝试用作:

echo $subCategory->getParentId();

回答by Jai

// to get the category ID by product id

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

回答by Jai

//Display all categories.

function get_categories(){

    $category = Mage::getModel('catalog/category'); 
    $tree = $category->getTreeModel(); 
    $tree->load();
    $ids = $tree->getCollection()->getAllIds(); 
    $arr = array();
        if ($ids){ 
            foreach ($ids as $id){ 
                $cat = Mage::getModel('catalog/category'); 
                $cat->load($id);
                $arr[$id] = $cat->getName();
        } 
}

return $arr;

}


 $arr =  get_categories();
 $arr = array_flip($arr);




//print the array 

Hope it will help someone.

希望它会帮助某人。