Iulian
Iulian

Reputation: 363

Magento get current product

I have a magento 1.7.0.2 , with varnish cache. I am trying to create an uncached block and to get the current product id on product view page, however neither Mage::registry('current_product')->getId(); , nor $this->getProduct()->getId(); seems to be working. My custom block looks like this, in catalog.xml :

        <catalog_product_view translate="label">
        ...
          <reference name="content">
          ...
            <block type="catalog/product_view" name="product.info.frz" template="catalog/product/view/frz.phtml">
                <action method="setEsiOptions">
                    <params>
                        <method>ajax</method>
                        <access>private</access>
                    </params>
                </action>
            </block>
          ...
          </reference>
        ...
        </catalog_product_view>

And I am displaying the block in product/view.phtml with <?php echo $this->getChildHtml('product.info.frz'); ?> Everything is working fine and the block doesnt get cached. However, when I'm trying to get the current product id , I get PHP message: PHP Fatal error: Call to a member function getId() on a non-object.

From what I know, Mage::registry('current_product')->getId(); should work on catalog/navigation blocks. Can you please poing me in the right direction ? What am I doing wrong ?

Thank you.

Upvotes: 1

Views: 8888

Answers (2)

Iulian
Iulian

Reputation: 363

It seems that I had to add registry for the ESI policy in order to access getId() method:

                    <registry_keys>
                        <current_product/>
                    </registry_keys>

Also, it seems that in my case, the ESI policies weren't working on 'catalog/navigation' , had to change it with 'core/template'

Hope this helps anybody.

Upvotes: 1

ant-workaholic
ant-workaholic

Reputation: 13

You must in your custom contoller and action where renderLayour(), add current product to registry;

public function viewAction()
 {
   $this->loadLayout();
   $id = $this->getRequest()->getParam('id');
   $current_product=Mage::getModel('catalog/product')->load($id);
   //Some code here
   Mage::register('current_product',$current_product);
   $this->renderLayout();
 }

Upvotes: 0

Related Questions