Websnippetz

Get cool snippets on php, css, jQuery, Magento and wordpress

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 🙂

karthi s

8 thoughts on “Modify item price in cart after placing order using sales_quote_add_item

  1. 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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: