swl1020
swl1020

Reputation: 823

Get productID of the product being edited in Magento

Is there a way, in Magento, to get the productID of the product currently being edited on the Admin backend?

I found this:

Mage::registry('current_product');

but it doesn't seem to get the one being edited.

Edit: If it makes any difference, I'm trying to get it from an extension listening for the event:

catalog_product_gallery_upload_image_after

Upvotes: 1

Views: 2300

Answers (2)

benmarks
benmarks

Reputation: 23205

That should work, as should Mage::registry('product'), as long as it is requested after Mage_Adminhtml_Catalog_ProductController::_initProduct() is called.

Mage::app()->getRequest()->getParam('id') should also work.

Edit based on event

The event being observed is dispatched in response to a JS-originated request. The controller action is unaware of the calling context, and there is no product registered in the registry. Logging the request params or possibly the referrer may give insight.

Upvotes: 1

1000Nettles
1000Nettles

Reputation: 2334

The current product's ID is in the URL. You can retrieve it as a parameter like so:

$productId = Mage::app()->getRequest()->getParam('id');

If you want to load the product as an object, store that ID and use

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

Edit: It looks like you're using an observer. The observer file itself I assume does not extend any part of Magento. (Is this the file? http://svn.magentocommerce.com/source/branches/1.7/app/code/core/Mage/Adminhtml/controllers/Catalog/Product/GalleryController.php) Try obtaining the action and getting the request that way

$action = $observer->getAction();
$productId = $action->getRequest()->getParam('id');

Upvotes: 4

Related Questions