So I have this requirement where I need to round product prices based on several criteria for one specific currency.
So when watching the catalog (category view / product view whatever) the prices need to be updated on the fly.
I managed to get it right by using the following observer:
<frontend>
<events>
<catalog_product_get_final_price>
<observers>
<update_price_on_the_fly>
<type>singleton</type>
<class>Vendor_Module_Model_Observer</class>
<method>roundPrice</method>
</update_price_on_the_fly>
</observers>
</catalog_product_get_final_price>
</events>
</frontend>
Then in my observer I did:
public function roundPrice(Varien_Event_Observer $observer) {
// check if are using NZD conversion
if (!preg_match("/NZD/i", $code = Mage::app()->getStore()->getCurrentCurrencyCode())) {
return;
}
$newPrice = $this->_getPriceLogic($observer->getProduct());
$observer->getProduct()->setFinalPrice($newPrice);
}
That works fine for the product view page but does not work on the category page. It seems like catalog_product_get_final_price
event is not dispatched on the category page.
I tried to observer catalog_product_collection_load_after
event and use the following code but it does not seem to work entirely.
Instead of displaying my rounded price is displays:
rounded price as low as original price
public function roundPriceCollection($observer) {
// check if are using NZD conversion
if (!preg_match("/NZD/i", $code = Mage::app()->getStore()->getCurrentCurrencyCode())) {
return;
}
$event = $observer->getEvent();
$products = $observer->getCollection();
foreach( $products as $product ) {
$product->setFinalPrice($this->_getPriceLogic($product));
}
return $this;
}
Thus I'm wondering what is the best approach to implement such feature.
catalog_product_get_final_price
method is not dispatched for collections. The recommendation from the core team was to usecatalog_product_collection_load_after
as you tried and it involved changing the attributeprice
in the base currency so the conversion ends up at a rounded value.catalog_product_collection_load_after
displaysrounded price as low as original price
price
andfinal_price
and other prices, so it will look like the original price is the rounded value.