1

How can I get the ids and the names of a group of parent categories? I got an array of ids that belong to a product but I want to show their parent id names too.

With this code I get all current ids:

$currentCatIds = $_product->getCategoryIds();

This returns an array:

Array ( [0] => 15 [1] => 25 [2] => 66 [3] => 78 [4] => 79 [5] => 80 [6] => 81 [7] => 82 [8] => 83 [9] => 84 [10] => 85 [11] => 86 [12] => 87 [13] => 88 [14] => 89 [15] => 90 [16] => 91 [17] => 100 [18] => 179 )

I need the parentids of the above categories and their names.

For example:

Topcategory 1
 - Subcat 1
 - Subcat 2
 - Subcat 3
 - Subcat 4
 - Subcat 5

Topcategory 2
 - Subcat 6
 - Subcat 7
 - Subcat 8

All subcats are in the above array and shown on the product page, but I need the ids of Topcategory 1 and 2 and their names.

1 Answer 1

1

To get all categories in magento 1.9.2.4 -

<?php  $_helper = Mage::helper('catalog/category'); $_categories = $_helper->getStoreCategories(); if (count($_categories) > 0){
    foreach($_categories as $_category){
        $_category = Mage::getModel('catalog/category')->load($_category->getId());
        $_subcategories = $_category->getChildrenCategories();
        if (count($_subcategories) > 0){
            echo $_category->getName();
            echo $_category->getId();      
            foreach($_subcategories as $_subcategory){
                 echo $_subcategory->getName();
                 echo $_subcategory->getId();
            }
        }
    } } ?>

To get a single category we should use -

<?php 
$category = Mage::getModel('catalog/category')->load(4); //4 = categoryID
$subcategories = $category->getChildrenCategories();
if (count($subcategories) > 0){
    echo $category->getName();
    foreach($subcategories as $subcategory){
         echo $subcategory->getName();
    }
}
?>

Hope this helps :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.