Search This Blog

Mar 21, 2011

Magento: Retrieve Category URL and Name from Product

Getting category Information such as Id, name, and url from its associated products is really simple. Using $product->getCategoryIds() code you can get all the id;s of your magento product category, then you’ll to do a foreach loop to get the all information you needed.


TRY THIS CODE BELOW.

<?php
/**
 * get categories from a product
 */
$allIds = $product->getCategoryIds();
/**
 * looping through the array of category ids
 */
foreach($allIds as $categoryId) {
 $category = Mage::getModel('catalog/category')->load($categoryId);
?>
<a href="<?php echo $category->getCategoryUrl() ?>"><?php echo$category->getName() ?></a><br/>
<?php
}
?>

If you have category in your magento site, the possible output will look like this.
category a
category b
category c

4 comments:

Kumar Abhinav said...

It would something like this, as this code is not working with above versions. Also there are some syntax errors.

//Load category Id from product
getCategoryIds();?>

//Looping for arrays
load($categoryIds);
}
?>
Your Link

I have implemented this in my code and its working well. All the Best!!!

Kumar Abhinav said...

I am not considering this wrong but its giving error on my codes. It would be something like this.

getCategoryIds();?>
load($categoryId);
//echo $category->getName()."
";
}
?>
Your Link

Anonymous said...

Hi! Thanks for helpfull information! I saw an interesting solution recently - a private sale script from Plumrocket. A number of magento extensions and magento private sales theme. And there you can find an extension URL Manager, that enhances Magento url rewrite management by allowing you to replace standard default links with those that are more search engine friendly thus being a great magento url rewriting technique. You can check this link https://store.plumrocket.com/magento-extensions/url-manager-magento-extension.html
Maybe you'll find it valuable for yourself.

jitender malav said...

i am not getting category url