简体   繁体   中英

Get Parent Category Id in Sub Category Page - Magento

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

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.

我得到了解决方案。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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