简体   繁体   English

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

[英]Get Parent Category Id in Sub Category Page - Magento

I need a small line of code. 我需要一小段代码。 I have a category like 我有类似的类别

  • Cat-1 CAT-1
    • Sub-Cat-1 子CAT-1
    • Sub-Cat-2 子的Cat-2
  • Cat-2 猫-2
    • Sub-Cat-3 子猫-3
    • Sub-Cat-4 子 - 猫-4-

In Sub-Cats page i need to get Root category's Id. 在Sub-Cats页面中,我需要获得Root类别的ID。 Like In "Sub-Cat-1" i need to get "Cat-1's" ID. 就像在“Sub-Cat-1”中一样,我需要获得“Cat-1”ID。 You can say in children category page, i need parent category's Id. 您可以在儿童类别页面中说,我需要父类别的ID。 act i am using short url like "abc.com/Sub-Cat-1", nither index.php nor root category in URL. 我正在使用像“abc.com/Sub-Cat-1”这样的短网址,无论是index.php还是URL中的根类别。 I am using Magento-1.4.1. 我正在使用Magento-1.4.1。 Please help me. 请帮我。 Thanks in advance. 提前致谢。

我得到了解决方案。

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

尝试用作:

echo $subCategory->getParentId();
// to get the category ID by product id

$productObj = Mage::getModel('catalog/product')->load($product_id);
$categoryIds =  $productObj->getCategoryIds();
//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. 希望它会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM