0

Is there a reliable way to determine a product from a given product url?

i.e.

http://mysite.com/clothes/tops/the-top.html

or

http://mysite.com/clothes/tops/the-top.html?___store=fr

or

http://mysite.com/clothes/tops/the-top

etc, etc

The point is that for any given valid product url, determine what product that belongs to.

Aside from the possible variations on urls, a couple listed above, there could be other variables i.e. stores, rewrites etc etc

  • 1
    Given what context? Are you needing to determine this apart from executing within the Magento request for serving the URL or needing to know when the page is loaded? – davidalger Sep 3 '13 at 16:27
  • This is from an import that i need to do where i a have only been given the urls and the data to update. From the data the url is the only thing i can possibly use to determine the product. I have a script that will parse the data csv and them attempt to load a product based on its url somehow and then update its data – Marty Wallace Sep 3 '13 at 16:40
  • Looks like others beat me to posting an answer… but given the context, I would say that @benubird's answer is the one you'll need. The best case scenario would be to convince the data-feed provider to include a SKU number as well as the URL. – davidalger Sep 3 '13 at 17:26
3

Yes. You need to separate out the actual part of the url that links to the page (in this case, clothes/tops/the-top.html), and look it up in the url rewrite rules. For programmatic reference, this is the table core_url_rewrite. You can find it in the backend under Catalog -> URL Rewrite Management.

2

I'm not sure of the use case, but in almost all of the situations in which you'd need to determine the current product you don't need to use the URL to do so:

$product = Mage::registry('current_product');

If not, you can do as @Benubird suggests and load the rewrite rule:

$urlKey = 'your/url/key.html';
$url = Mage::getModel('core/url_rewrite')->getCollection()
    ->addFieldToFilter('request_path',array('eq'=>$urlKey))
    ->addFieldToFilter('product_id',array('notnull'=>true));


$url->load();
var_dump($url->getData());

Your product ids with a matching url are available in $url->getProductId();

If no matches are found, $url->getData() will be an empty array.

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.