I have bought a theme that allows a custom tab in product view. It uses a static block for this. Now I have some products that come with documentation in pdf format. I was thinking I want to add links to the documentation in that custom tab. So I've created widgets before and I thought the easiest way was to create a widget that puts out the links, and place it in the static block used for the custom tab. That way I don't have alter theme files.
Now I managed to create the widget and have it render in the custom tab. So far so good. Now I want to display the links to the pdf's (which I have as a product attribute), but I can't find a way to get the current product's attributes (that is the product viewed on the frontend) within the widget (class).
I would like to know a way to get the id or model of the product being viewed, or something alike, so I can get the product attributes. This, of course, within the widget class.
I am currently using Magento CE 1.9.1.1
Some thoughts on this? I have searched the deepest of the interwebs but wasn't able to find anything. Hope someone can get me on the right track. Thanks in advance.
2 Answers 2
Simply run the following code. No need to load the product using request parameters, because the product model should already be initiated and loaded:
if(Mage::registry('current_product')) { //check if product global 'registry' object is available
 $product = Mage::registry('current_product');
 $name = $product->getName(); //etc
} 
If you're loading the product model twice, you will use twice the resources.
I did some more digging and found out it's quite simple:
First I check whether or not on a product page, if so then load the product:
if(Mage::registry('current_product')) { // <-- check if on product page
 $productId = Mage::app()->getRequest()->getParam('id'); // <-- get current product Id
 $product = Mage::getModel('catalog/product')->load($productId); // <-- Load product
} 
- 
 No need to load the product,Mage::registry('curent_product')should already be a product objectMilan Simek– Milan Simek2015年05月10日 13:00:58 +00:00Commented May 10, 2015 at 13:00