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?

4

Try this:

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

Then in your loop try using getUrl() method.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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