Magento – Update product price before placing order

event-observermagento-1.9orders

I am running a Magento site for selling Gold coins. As the Gold rate change on the regular basis, I am updating the product price on the regular basis.

I want to customise the checkout page, so that I receive the order with latest Gold rate. That means, price of the product get update just after submitting the order.

Questions

  • which event must I observe? (experiences..?)
  • how do I update the price?
  • Is it better to update the real product price instead of the quote?

Any one please suggest, what would be the best approach and how I can customise the checkout query to update the product price with Gold API ?

Here's my code for custom checkout:

CustomPriceUpdate_Checkout.xml
Path: app/etc/modules/CustomPriceUpdate_Checkout.xml

    <?xml version="1.0"?>
<config>
    <modules>
        <CustomPriceUpdate_Checkout>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </CustomPriceUpdate_Checkout>
    </modules>
</config>

Config.xml
Path: app/code/local/CustomPriceUpdate/Checkout/etc/config.xml

    <?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <CustomPriceUpdate_Checkout>
        <version>0.0.1</version>
    </CustomPriceUpdate_Checkout>
</modules>
<global>
    <models>
        <myCustomPriceUpdate_Checkout>
            <class>CustomPriceUpdate_Checkout_Model</class>
        </myCustomPriceUpdate_Checkout>
    </models>
    <events>
        <sales_order_place_before>
            <observers>
                <modify_product_price>
                    <type>singleton</type>
                    <class>myCustomPriceUpdate_Checkout/observer</class>
                    <method>update_price</method>
                </modify_product_price>
            </observers>
        </sales_order_place_before>
    </events>
</global>
</config>

Observer.php:
Path: app/code/local/CustomPriceUpdate/Checkout/Model/Observer.php

 <?php
class CustomPriceUpdate_Checkout_Model_Observer{
  public function update_price($observer){
    $quote = $observer->getEvent()->getQuote();
   foreach ($quote->getAllItems() as $item) {
       $product = $item->getProduct();//if you need it
       $additional = 23;
       $finalPrice = $item->getOriginalPrice() + $additional;
       $item->setCustomPrice($finalPrice);
       $item->setOriginalCustomPrice($finalPrice);
       $item->save();
     //your magic here.
    }
    $quote->save();  
    Mage::log("Order is placed");
  }
}

My observer is working fine, as I can able to print the text in the log file by changing the observer to this:

class CustomPriceUpdate_Checkout_Model_Observer{
  public function update_price($observer){
    Mage::log("Order is placed");
  }
}

But my complete code not working. I am very sure my code in the observer is not correct.

Please someone correct me.

Best Answer

You can use Event/Observer to check any price update.

  1. sales_order_place_before ( This event dispatch before place order ).
  2. sales_order_place_after ( This event dispatch after place order ).
  3. Once customer can be placed order there after you can't do anything for the price of that order.
Related Topic