Modify item price in cart after placing order using sales_quote_add_item

We can easily modify product price after placing order in magento using the event sales_quote_add_item.

We can see this hook registered in app/code/core/Mage/Sales/Model/Quote.php line:874. This is the event created by magento.
Mage::dispatchEvent('sales_quote_add_item', array('quote_item' => $item));

We can access $item values using its registered event name sales_quote_add_item and we can modify the price with our logic.

Follow the steps:

  1. Create a new module. Register our module in magento.
    Create a file in app/etc/modules/Company_All.xml

    <?xml version="1.0"?>
    <config>
    <modules>
    <Company_Product>
    <codePool>local</codePool>
    <active>true</active>
    </Company_Product>
    </modules>
    </config>
  2. Create configuration file for our module file in app/code/local/Company/Product/etc/config.xml

    <?xml version="1.0"?>
    <config>
    <global>
    <models>
    <product>
    <class>Company_Product_Model</class>
    </product>
    </models>
    <events>
    <sales_quote_add_item><!--Event to override price after adding product to cart-->
    <observers>
    <company_product_price_observer><!--Any unique identifier name -->
    <type>singleton</type>
    <class>Company_Product_Model_Price_Observer</class><!--Our observer class name-->
    <method>update_book_price</method><!--Method to be called from our observer class-->
    </company_product_price_observer>
    </observers>
    </sales_quote_add_item>
    </events>
    </global>
    </config>
  3. Create our observer file in app/code/local/Company/Product/Model/Price/Observer.php

    class Company_Product_Model_Price_Observer{
    public function update_book_price(Varien_Event_Observer $observer) {
    $quote_item = $observer->getQuoteItem();

    //if(){ //your logic goes here
    $customprice = 50;
    //}
    $quote_item->setOriginalCustomPrice($customprice);
    $quote_item->save();
    return $this;
    }

    }

Is this post helpful? Share your comments :-)

Comments

  1. [...] Add the below code in your module config.xml. If you want to know hoe to create module refer Magento event module creation post [...]

    ReplyDelete
  2. nice stuff! works for me!

    ReplyDelete
  3. Thanks for commenting inder :)

    ReplyDelete
  4. I have to add custom price of product in cart. Product price is calculated for per square feet. Please tell me how can i build module.

    for eg. if custom enter between 1-400sq ft then price will calculated and that price will add to cart.

    Similar to this site http://www.builddirect.com/Hardwood-Flooring/Natural/ProductDisplay_6950_p1_10075949.aspx?bdps=100fpp2.

    Please sort out my problem. i have done calculation of price by after calculation product changed price will not add to cart. Help me to add the calculated price in cart.

    ReplyDelete
  5. Currency is not working for multiple currency....

    ReplyDelete
  6. […] Add the below code in your module config.xml. If you want to know hoe to create module refer Magento event module creation post […]

    ReplyDelete
  7. You're right! Did you sort this out?

    ReplyDelete
  8. Great Stuff, exactly what I wanted. Thanks a ton.

    ReplyDelete

Post a Comment

Popular posts from this blog

Send email from xampp localhost