Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento get formatted product URL

Tags:

magento

I'm trying to get the formatting product url from a Magento product collection.

So I want https://www.myshop.co.uk/red-bouncy-ball.html

Specifically the red-bouncy-ball part.

I've tried this method:

$categoryId = $product->getCategoryIds();
$url = $product->setCategoryId($categoryId)->getProductUrl();

Which gives me:

https://www.myshop.co.uk/index.php/catalog/product/view/id/100/

and just

$product->getProductUrl()

which does the same.

like image 349
BobFlemming Avatar asked Dec 17 '25 22:12

BobFlemming


1 Answers

Are you simply looking for the 'red-bouncy-ball' part of the url?

If so then...

$product->getData('url_key');

When initialising your collection you will need to add the url_key to the select statement...

addAttributeToSelect('url_key')

like image 185
Drew Hunter Avatar answered Dec 20 '25 18:12

Drew Hunter