Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Load product by url

Tags:

magento

How would one load a product model in Magento, if the product id is not available and only the product's url is? For example, I want to retrieve the product model from it's friendly url, such as

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 doesn't seem to work correctly. The Magento documentation is very lacking in this area; does anyone know how to accomplish this?

like image 679
Nick Daugherty Avatar asked Feb 13 '12 02:02

Nick Daugherty


People also ask

How to find product URL in Magento?

If you need to get current URL in Magento 2 PHTML file the easiest way to do this is to use the following code: $currentUrl = $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);

How do I get a product URL?

You can be assigned Product URL to any page in store for a redirect to Product page. If you want to get Only Url Key value, You can access by $product->getUrlKey() method. You can product full URL by getProductUrl() method.

How to get product data from ID in Magento 2?

Step 2: Get product by ID or SKU in template file $id = YOUR_PRODUCT_ID; $sku = 'YOUR_PRODUCT_SKU'; $_product = $block->getProductById($id); $_product = $block->getProductBySku($sku); echo $_product->getEntityId(); echo '<br />'; echo $_product->getName();


2 Answers

Here's an alternative solution.

First use the URL rewrite model to find the route which 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 locate the produc's id:

    $iProductId = $oRewrite->getProductId();

Finally if you require the product model object itself it's then a simple matter to call:

    $oProduct = Mage::getModel('catalog/product')->load($iProductId);

The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.

The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.

like image 119
Jim OHalloran Avatar answered Sep 28 '22 12:09

Jim OHalloran


I'm curious as to why you need to do this, since this might not be the best solution. This should be easy enough using the addAttributeToFilter() method on 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;
like image 43
Zachary Schuessler Avatar answered Sep 28 '22 10:09

Zachary Schuessler