0

I have url string. I need to detect if url belongs to product or category and get product or category id.

I tried this, but it won't work:

  $vPath = 'http://dev.magento186.com/default/example/example3.html';
  $oRewrite = Mage::getModel('core/url_rewrite')
                  ->setStoreId(Mage::app()->getStore()->getId())
                  ->loadByRequestPath($vPath);
  var_dump($oRewrite->getProductId());

It works only if $vPath is path and not full url. Is there any easy way to get product id or category id from full url?

0

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.

  • I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin. – JohnyFree Oct 31 '17 at 21:15
0

After researching I didn't find any better solution so I ended up with this:

  $urlString = "http://dev.magento186.com/default/example/example3.html?example=1";
  $url = Mage::getSingleton('core/url')->parseUrl($urlString);
  $path = $url->getPath();
  $storeCode = Mage::app()->getStore()->getCode();
  $path = str_replace("/".$storeCode."/", "", $path);
  $vPath = str_replace("//", "", $path);

Now I get proper url path, I can use this:

  $oRewrite = Mage::getModel('core/url_rewrite')
              ->setStoreId(Mage::app()->getStore()->getId())
              ->loadByRequestPath($vPath);
  var_dump($oRewrite->getProductId());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.