Efficient get product url from id

Interesting to know if we can get the product URL without loading whole product. Sorry to answer your additional question and not the main question, as I am also not sure if we can get it :)

For getting a single attribute without loading whole product, you can use getAttributeRawValue() if you are using Magento CE version 1.6+

Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);

As per @philwinkle's comment below, here is more optimized code:

Mage::getResourceSingleton('catalog/product')
  ->getAttributeRawValue($productId, 'attribute_code', Mage::app()->getStore());

UPDATE: Get product URL by ID without loading whole product:

Mage::getResourceSingleton('catalog/product')
  ->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore());

I think it would be beneficial to do this via a collection. If you do not need the complete product information then you could load it via a collection and so only load information you need. The code would be as follows.

$product_collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter('entity_id', 16)
    ->addUrlRewrite();
$product_collection_url = $product_collection->getFirstItem()->getProductUrl();

Note: the addUrlRewrite so that you can get the product url later, this can also take a category id as a parameter, without this the url would not contain the category.

You are still end up calling the getProductUrl() which ends up using Mage_Catalog_Model_Product_Url, but this only needs the id, request_path and the url key so it should work without the full product.


I guess the most efficent way to get a products rewritten URL (w/o loading product) is to look directly on URL rewrite talbe.

# returns string "product-url.html"
$productUrl = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/' . $productId, $storeId);

To get full category path, use

# returns string "full/category/path/product-url.html"
$productUrl = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/' . $productId . '/' . $categoryId, $storeId);

Edit: tested for "url_key", for other attributes getAttributeRawValue should fit.

  1. getRequestPathByIdPath

    • Total Incl. Wall Time (microsec): 8,323 microsecs
    • Total Incl. CPU (microsecs): 8,038 microsecs
    • Total Incl. MemUse (bytes): 273,872 bytes
    • Total Incl. PeakMemUse (bytes): 213,488 bytes
    • Number of Function Calls: 372
  2. getAttributeRawValue

    • Total Incl. Wall Time (microsec): 51,213 microsecs
    • Total Incl. CPU (microsecs): 50,390 microsecs
    • Total Incl. MemUse (bytes): 1,223,520 bytes
    • Total Incl. PeakMemUse (bytes): 1,168,496 bytes
    • Number of Function Calls: 2,233
  3. load(productId)

    • Total Incl. Wall Time (microsec): 605,649 microsecs
    • Total Incl. CPU (microsecs): 598,820 microsecs
    • Total Incl. MemUse (bytes): 5,370,848 bytes
    • Total Incl. PeakMemUse (bytes): 5,314,384 bytes
    • Number of Function Calls: 27,774