Modify product price dynamically on the go – Magento

We all know that Magento is an excellent eCommerce solution right now. It have a pretty big community support & extension database. Even though everything was running perfectly sometimes we might need to customize and get our hands on source code. In this post I am explaining such a situation. Please keep in mind that this targets only to give you a basic idea about the solution rather than giving a copy-paste result.

Task :

Modify product price dynamically on the go. Price will not be changed in inventory. It will changed only for the front-end display. Customer can place the order on the new price too.

Solution :

There are three places were prices are displayed in front-end. On product collections page (categories, search..etc), On product single page and in cart. So we need to use three observers here. They are rewrite_finalprice_product, catalog_product_collection_load_after and checkout_cart_product_add_after. So configure your config.xml file like below :

<catalog_product_load_after>
<observers>
<rewrite_finalprice_product>
<type>singleton</type>
<class>mycustompricemodule/observer</class>
<method>rewriteFinalPrice</method>
</rewrite_finalprice_product>
</observers>
</catalog_product_load_after>
<catalog_product_collection_load_after>
<observers>
<rewrite_finalprice_product_collection>
<type>singleton</type>
<class>mycustompricemodule/observer</class>
<method>rewriteFinalPriceCollection</method>
</rewrite_finalprice_product_collection>
</observers>
</catalog_product_collection_load_after>
<checkout_cart_product_add_after>
<observers>
<unique_event_name>
<class>mycustompricemodule/observer</class>
<method>modifyCartFinalPrice</method>
</unique_event_name>
</observers>
</checkout_cart_product_add_after>

Now you need to prepare the classes for the three observers. Basic structure below :

/*For single product*/
public function rewriteFinalPrice($observer)
{
//$observer have $product data this function is for single product
$product = $observer->getEvent()->getProduct();
$newprice = 'Your new price';
//These two actions will set the product price
$product->setMinimalPrice($intact_sprice);
$product->setFinalPrice($intact_sprice);
}

/*For multiple product page*/
public function rewriteFinalPriceCollection($observer)
{
//$observer have $collection data. Remember this function is for multiple product page. So you have to loop $collection variable.
$collection = $observer->getCollection();
foreach($collection as $_product) {
$newprice = 'Your new price';
//These two actions will set the product price
$_product->setMinimalPrice($intact_sprice);
$_product->setFinalPrice($intact_sprice);
}
}

/*Cart display*/
public function modifyCartFinalPrice(Varien_Event_Observer $obs)
{
// Get the quote item
$item = $obs->getQuoteItem();
// Ensure we have the parent item, if it has one
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$newprice = 'Your new price';
$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
}

That’s it. It does the job.