Magento - Download Product at URL

How to upload a product model to Magento if the product identifier is not available and only the product URL? For example, I want to extract a product model from this friendly URL, for example

electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html 

I found the following code in another post :

 $oRewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath( $path ); 

but it does not work correctly. Magento documentation is sorely lacking in this area; Does anyone know how to do this?

+8
magento
source share
2 answers

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.

+19
source share

I wonder why you need this, as this may not be the best solution. This should be simple enough with the addAttributeToFilter () method in the collection:

 $path = 'folder/folder/my-product-name'; // Get the product permalink $productName = explode('/', $path); $productName = end($productName); // Filter the url_path with product permalink $products = Mage::getModel('catalog/product')->getCollection(); $products->addAttributeToFilter('url_path', $productName) ->getFirstItem(); Zend_Debug::dump($products->getData());exit; 
+3
source share

All Articles