down
down

Reputation: 119

get the parent category ids in magento

i put the following code into magento base template file view.phtml.

$categories = $_product->getCategoryIds(); print_r($categories)

with the sample data,when on the Ottoman product page,which breadcrumb is

 Home / Furniture / Living Room / Ottoman

the output is Array ( [0] => 22 ) the 22 is Living Room id. how to get Furniture and Living Room id. thank you

Upvotes: 0

Views: 4931

Answers (2)

zuzuleinen
zuzuleinen

Reputation: 2634

Here is how you can get all categories id's:

//load root category 
$rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$rootCategory = Mage::getModel('catalog/category')
                ->load($rootCategoryId);

//load all children categories of the root category
$childrenCategories = $rootCategory->getChildrenCategories();

Now you can have access to their ids like so:

foreach ($childrenCategories as $category) {
        var_dump($category->getId());
}

Upvotes: -1

blakcaps
blakcaps

Reputation: 2647

$categories=$_product->getCategoryIds();
foreach ($categories as $category)
{
    $info = Mage::getModel('catalog/category')
            ->load($category)
            ->getParentCategory();
    echo $info->getId(); //display category ID
    //var_dump($info->debug());  # display model contents
}

Upvotes: 2

Related Questions