0

I need to display all categories from one product on product view page.

This code I found, but it displays one category per line:

<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?> 
< <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>
<?php endforeach; ?>

But I need it this way:
cat 1
cat 2 > subcat 1
cat 3 > subcat 1 > subcat 2

1

1 Answer 1

1

Try below code.

<?php 
$categories = $_product->getCategoryIds();
if (count($categories) > 0){
    foreach($categories as $k => $_category_id){
        $_category = Mage::getModel('catalog/category')->load($_category_id);
        echo $_category->getName();
        echo $_category->getId();
        $_subcategories = $_category->getChildrenCategories();
        if (count($_subcategories) > 0){      
            foreach($_subcategories as $_subcategory){
                 echo $_subcategory->getName();
                 echo $_subcategory->getId();
            }
        }
    }
}
?>

Your Answer

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

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