How to Get Current Product Attributes on Product View Page Using Widget

productwidget

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.

Best Answer

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.

Related Topic