php - Magento Block Class Rewrite In Config.xml - Second Guessing My Methodology -


so have written module works quite well. however, trying make sure write of modules conflict-free possible. short: module displays reviews associated products of grouped , configurable products on parent product page , listings.

in catalog/product/list.phtml there 3 lines of code if parent product has no reviews, though children products have reviews, not satisfy if condition in order display summary html.

<?php if($_product->getratingsummary()): ?> <?php echo $this->getreviewssummaryhtml($_product) ?> <?php endif; ?> 

so, goal add data each element in collection before reached template file.

$_productcollection=$this->getloadedproductcollection(); 

so, did rewrite of mage_catalog_block_product_list , rewrote method below.

protected function _getproductcollection() 

actually, copied exact method on , added simple code bottom of , indeed want do.

protected function _getproductcollection() {   if (is_null($this->_productcollection)) {       $layer = $this->getlayer();       /* @var $layer mage_catalog_model_layer */       if ($this->getshowrootcategory()) {           $this->setcategoryid(mage::app()->getstore()->getrootcategoryid());       }        // if product view page       if (mage::registry('product')) {           // collection of categories product associated           $categories = mage::registry('product')->getcategorycollection()               ->setpage(1, 1)               ->load();           // if product associated category           if ($categories->count()) {               // show products category               $this->setcategoryid(current($categories->getiterator()));           }       }        $origcategory = null;       if ($this->getcategoryid()) {           $category = mage::getmodel('catalog/category')->load($this->getcategoryid());           if ($category->getid()) {               $origcategory = $layer->getcurrentcategory();               $layer->setcurrentcategory($category);               $this->addmodeltags($category);           }       }       $this->_productcollection = $layer->getproductcollection();        $this->preparesortablefieldsbycategory($layer->getcurrentcategory());        if ($origcategory) {           $layer->setcurrentcategory($origcategory);       }   }    foreach($this->_productcollection $product){     if(($product->gettypeid() == 'configurable' || $product->gettypeid() == 'grouped')      && !$product->getratingsummary()) {         $product->setratingsummary(1);     }   }    return $this->_productcollection; } 

what cant second guess need rewrite function add data collection returns or there way add data collection before reaches template file without doing actual rewrite have done above. category product list 1 of main data collections on magento site , not crazy messing add tiny snippet of data. also, worry conflicts future extensions.

please let me know how alter collection object without rewrite, if possible.

thank you.

if going rewrite, i'd recommend methodology this.

protected function _getproductcollection() {     $collection = parent::_getproductcollection();     //do stuff collection here     return $collection; } 

your block class extends existing block class. means can call parent method , have same code run. when copy , paste code did in example, risk extension not working in past/future versions of magento code has changed.

my general rule of thumb on using rewrites is: if stay in control of code , can debug potential conflicts, rewrite fine. if don't stay in control of code, shouldn't rewrite method.

if want avoid rewrite, other option magento's event system. every collection in magento fires event before , after it's loaded. trick finding name of event you're after, requires either digging through source of mage::dispatch, or using tool like commerce bug (warning: self link) track down events your'e after.

i prefer second method, using commerce bug's observer's tab (which shows existing magento observers setup on page), can see magento has listener setup product collection. specifically, on catalog_product_collection_load_after event.

enter image description here

if @ source of magento's cataloginventory/observer, can see how magento modifies collection after been loaded

#file: app/code/core/mage/cataloginventory/model/observer.php public function addstockstatustocollection($observer) {     $productcollection = $observer->getevent()->getcollection();     if ($productcollection->hasflag('require_stock_items')) {         mage::getmodel('cataloginventory/stock')->additemstoproducts($productcollection);     } else {         mage::getmodel('cataloginventory/stock_status')->addstockstatustoproducts($productcollection);     }     return $this; } 

if case, instead of checking require_stock_items flag on products collection, check state of system says it's loading page want information added.

that should give enough started own listener. luck!


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -