Magento 1.8 – How to Get Product Category URL Key

categorycategory-productsmagento-1.8

How can I get the product category's URL Key?

I can get the category name by $category->getName() but it does not work if I use this $category->getURLKey(),

$categories = $_product->getCategoryCollection()
    ->addAttributeToSelect('name');

foreach($categories as $category) {
    $productCategoryName = $category->getName();
    var_dump($category->getURLKey());
}

Return null

Any ideas?

Best Answer

Try this:

$categories = $_product->getCategoryCollection()
    ->addAttributeToSelect('name')
    ->addUrlRewriteToResult();

Then in your loop try using getUrl() method.

Related Topic