Here is an alternative solution.
First use the URL rewrite model to find the route that matches your product:
$vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html'; $oRewrite = Mage::getModel('core/url_rewrite') ->setStoreId(Mage::app()->getStore()->getId()) ->loadByRequestPath($vPath);
Then you can call getProductId () on the route to find the produc file:
$iProductId = $oRewrite->getProductId();
Finally, if you need the product model object itself, then just call:
$oProduct = Mage::getModel('catalog/product')->load($iProductId);
The main difference between the above and the code example you provided is the call to setStoreId. The same product can have different URLs, depending on which store it is in, so there must be an appropriate store context for the routing component before it can find the displayed product.
The advantages of this Zachary Schuessler solution are that using a URL rewriter will determine the correct product each time the end parts of the URL are the same for different products (e.g. folder1 / my-product-name and folder2 / my -product-name - different products). Using URL rewriting also works in situations where "folder1 / my-product" refers to different products in different stores. This may or may not apply to your environment.
Jim ohalloran
source share