Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: None  |  size: 1.58 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Magento pass current product ID to module
  2. Mage::registry('current_product');
  3.        
  4. protected function _getIdentifier()
  5. {
  6.     return $this->_getCookieValue(Enterprise_PageCache_Model_Cookie::COOKIE_CUSTOMER, '');
  7. }
  8.        
  9. protected function _getCacheId()
  10. {
  11.     return 'HOMEPAGE_PRODUCTS' . md5($this->_placeholder->getAttribute('cache_id') . ',' . $this->_placeholder->getAttribute('product_id')) . '_' . $this->_getIdentifier();
  12. }
  13.        
  14. public function getCacheKeyInfo()
  15. {
  16.     $info = parent::getCacheKeyInfo();
  17.     if (Mage::registry('current_product'))
  18.     {
  19.         $info['product_id'] = Mage::registry('current_product')->getId();
  20.     }
  21.     return $info;
  22. }
  23.        
  24. protected function _renderBlock()
  25. {
  26.     $blockClass = $this->_placeholder->getAttribute('block');
  27.     $template = $this->_placeholder->getAttribute('template');
  28.  
  29.     $block = new $blockClass;
  30.     $block->setTemplate($template)
  31.         ->setProductId($this->_placeholder->getAttribute('product_id'));
  32.     return $block->toHtml();
  33. }
  34.        
  35. protected function _initAction(){
  36.  
  37.     if(! Mage::registry('current_product')){
  38.         Mage::register('current_product', Mage::getModel('catalog/product'));
  39.     }
  40.  
  41.     $id = $this->getRequest()->getParam('id');
  42.     if (!is_null($id)) {
  43.         $model = Mage::registry('current_product')->load($id);
  44.  
  45.         if (! $model->getId()) {
  46.             $this->_getSession()->addError(Mage::helper('catalog')->__('This product item no longer exists'));
  47.             $this->_redirect('*/*/');
  48.             return;
  49.         }
  50.     }
  51.  
  52.     return $this;
  53. }
  54.  
  55. public function indexAction()
  56. {
  57.   $this->_initAction();
  58.   // your code below
  59. }