Yet another best practice question.
So here's the thing, I have several modules that deal with setting different data on the core Magento block.
I don't like to rewrite entire blocks and I tend to use observers intead.
Here's an example:
The goal of the module is to set some rates for the following blocks:
Mage_Checkout_Block_Onepage_Shipping_Method_Available
Mage_Checkout_Block_Cart_Shipping
The easiest way of doing it would be to rewrite both blocks to add my logic in the getEstimateRates
method but I don't really like that logic so here's what I've done:
Still rewritten the blocks
Just to add the following method that is missing from the original blocks:
public function setEstimateRates($rates)
{
$this->_rates = $rates;
}
Use observers
Thanks to this new method, I can use an observer on core_layout_block_create_after
to update the rates on the fly:
// Get the block
$block = $observer->getBlock();
// Get the OG block rates
$rates = $block->getEstimateRates();
if ()
{
unset($rates[self::$_KEY]);
}
// Set the new rates
$block->setEstimateRates($rates);
A second example of a similar problem I have faced a while ago can be found here: Category Product List : add a custom view mode via observer
So here are my questions:
- is that bad practice to add methods to native classes, especially setters ?
- is it better practice to do what I just did instead of rewriting a core class method ? pros/cons ?