Close

November 3, 2014

Magento EE Load Product By URL Key

Just a quick tip. If you are using Magento Enterprise and finding that “loadByRequestPath” doesn’t always work when you try and load a product by the url key, it is because it’s a “feature”. According to Magento Enterprise support, Magento EE, uses its own urlrewrite model.

Magento now uses enterprise_urlrewrite/url_rewrite model to get url rewrite model. So if you need to load the product by url key, he suggested that you can use the following code:

 
$urlKey='my-special-url-key';
$productsCollection = Mage::getModel('catalog/product')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->getCollection();
$productEntity = $productCollection->addAttributeToFilter('url_key', $urlKey)->getFirstItem();

Voila, everything works again. What was really tough to debug, was that our initial use of the ‘loadByRequestPath’ was actually bringing back a few products in the call. We thought it was just that our indexes on the site were having issues.