I have a product object and I need to get product URL of it that will include all its categories e.g. - http://example.com/category/subcategory/product_name
I've managed to find a way that will return to me such type of url. Here is is:
// $product - is defined
$categoryIds = $product->getCategoryIds();
$cats_collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('entity_id', $categoryIds)
->addAttributeToFilter('is_active', 1);
// I filter the categories by their level, so they always go cat/sub-cat/sub-sub-cat
$cats_collection->getSelect()->order('level DESC')->limit(1);
// I get a product URL that looks like http://example.com/product_name
$productURL = $product->getProductUrl();
// I get just product_name part
$productSlug = substr( $productURL, strrpos( $productURL, '/' )+1 );
// http://example.com/cat/sub-cat/sub-sub-cat
$categoryUrl = $cats_collection->getFirstItem()->getUrl();
// Final result: http://example.com/cat/sub-cat/sub-sub-cat/product_name
echo $categoryUrl . '/' . $productSlug;
But my guts are telling me that this can be done simpler.
Is there any simpler way of getting such URL? Or is it possible to optimize some part of my code?
Thank you.