If you are going to do a rewrite, I'd recommend a methodology like this.
protected function _getProductCollection()
{
$collection = parent::_getProductCollection();
//do your stuff to the collection here
return $collection;
}
Your block class extends the existing block class. That means you can call the parent
method and have the same code run. When you copy and paste code as you did in your example, you risk your extension not working in past/future versions of Magento where this code has changed.
My general rule of thumb on using rewrites is: If you stay in control of the code and can debug potential conflicts, a rewrite is fine. If you don't stay in control of your code, you shouldn't rewrite a method.
If you want to avoid a rewrite, your other option is Magento's event system. Every collection in Magento fires an event before and after it's loaded. The trick is finding the name of the event you're after, which requires either digging through the source of Mage::dispatch
, or using a tool like Commerce Bug (warning: self link) to track down the events your'e after.
I prefer the second method, so using Commerce Bug's observer's tab (which shows any existing Magento observers setup on a page), we can see Magento itself has a listener setup for the product collection. Specifically, on for the catalog_product_collection_load_after
event.
If you look at the source of Magento's cataloginventory/observer
, you can even see how Magento modifies the collection after its 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 your case, instead of checking the require_stock_items
flag on the products collection, check for something about the state of the system that says it's loading the page where you want your information added.
That should give you enough to get started with your own listener. Good luck!