4

Here is a sample url from my company's Magento site (and we're obviously using stores in the URL hence the smi000.. code)

http://shop.mystore.com/smi130495/catalog/product/view/id/35830/s/men-s-traditional-ring/category/312834/

I understand that the category id is 312834, and the product id is 35830. I understand how to use Mage::helper->getURL() to get the URL, but how do I get the product ID using magento code (i.e. the "Magento Way"?)

4

You could get the product id from the core_url_rewrite table using the url for this, try this:

$url = Mage::helper->getURL();
$rewrite = Mage::getModel('core/url_rewrite')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByRequestPath($url);

$productId = $rewrite->getProductId();

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

Also you could get the id from the url directly (no extra query to the DB) like this:

$productId = $this->getRequest()->getParam('id');

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.