Set Custom Price in magento shopping cart page usefull code snippet

Create custom module Custom_Price in magento Directory
make Observer.php file in module and paste below code 
<?php
class Custom_Price_Model_Observer
{
    public function modifyPrice(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 );
        // Load the custom price
      //    $_item = $obs->getItem();
          $_product= Mage::getSingleton('catalog/product')->load($item->getProductId());
        $weight= $_product->getResource()->getAttribute('weight')->getFrontend()->getValue($_product);
        $reg =  $_product->getResource()->getAttribute('price')->getFrontend()->getValue($_product);
        $price =$reg + $weight*0.35; // you can set your custom price here whatever you want
        // Set the custom price
        $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        // Enable super mode on the product.
        $item->getProduct()->setIsSuperMode(true);
    }

   
}
?>
Now make changes in config.xml
put below code between <frontend> tag
 <events>
            <checkout_cart_product_add_after>
                <observers>
                    <unique_event_name>
                        <class>price/observer</class>
                        <method>modifyPrice</method>
                    </unique_event_name>
                </observers>
            </checkout_cart_product_add_after>
        </events>


Comments