3

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?

0

1 Answer 1

4

Try this:

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

Then in your loop try using getUrl() method.

4
  • thanks but it return the hittp url, such as http://localhost/mystore/bin/app/magento/en/wine/ instead of wine.
    – Run
    Jul 2, 2014 at 21:59
  • 3
    I did it with ->addAttributeToSelect('url_key') and then $category->getUrlKey() then i get wine for instance. thanks!
    – Run
    Jul 2, 2014 at 22:01
  • 1
    Ah sorry, I read it wrong. Thought you needed a complete URL, not just URL key.
    – user487772
    Jul 2, 2014 at 22:03
  • No worries. You have given me a very good hint for doing that! :-)
    – Run
    Jul 2, 2014 at 22:14

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.