1

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.

1
  • Anyone with some suggestion ? Jan 12, 2017 at 11:29

4 Answers 4

4

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.
1
  • I am using the same approach that you mentioned, also I update my question with the code I am using for custom checkout, but its not working for me. Can you please check what I am doing wrong ? Jan 6, 2017 at 7:04
0

Try this in your observer

 $product->setOrginalCustomPrice(<your_price>);

Instead of

$product->setPrice(<YourPrice>);
1
  • I changed by observer to this class CustomCheckout_Event_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->getProduct()->setIsSuperMode(true); //your magic here. } } } But its still not working. Jan 6, 2017 at 8:51
0

Had recently a similar problem, and couldn’t find somewhere a good solution for it. Here what I have done.

I rewrited the method “saveOrderAction” in the checkout Controller “Mage_Checkout_OnepageController ” dispatching a custom event.


config.xml

<frontend>
        <routers>
            <checkout>
                <args>
                    <modules>
                        <w2_rewrite before="Mage_Checkout">W2_Rewrite</w2_rewrite>
                    </modules>
                </args>
            </checkout>
        </routers>
    </frontend>


controller/OnepageController.php

<?php

require_once(Mage::getModuleDir('controllers','Mage_Checkout').DS.'OnepageController.php');

class Vendor_Module_OnepageController extends Mage_Checkout_OnepageController
{
    /**
     * Create order action
     */
    public function saveOrderAction()
    {
        if (!$this->_validateFormKey()) {
            $this->_redirect('*/*');
            return;
        }

        Mage::dispatchEvent('checkout_onepage_controller_custom_save_order', array('quote' => $this->getOnepage()->getQuote()));

        parent::saveOrderAction();
    }
}


After that I created an Observer, which is observing this event. In this method I modified the products in the quote and recalculated the totals.

So after the submit of the checkout button, the new prices will pass to the order with all the prices calculations needed. Here is the implementation.


config.xml

<global>
       ...     
       <events>           
           <checkout_onepage_controller_custom_save_order>
                <observers>
                    <vendor_module>
                        <type>model</type>
                        <class>vendor_module/observer</class>
                        <method>checkoutOnepageControllerCustomSaveOrder</method>
                    </vendor_module>
                </observers>
            </checkout_onepage_controller_custom_save_order>
        </events>
    </global>


Observer.php

    /**
     * @param Varien_Event_Observer $observer         
     */
    public function checkoutOnepageControllerCustomSaveOrder(Varien_Event_Observer $observer)
    {          

        $quote =  $observer->getEvent()->getQuote();    

        foreach ($quote->getAllItems() as $item) {    

            $calculatedPrice = $this->_getPriceLogic($item);    

            $item->setCustomPrice($calculatedPrice);
            $item->setOriginalCustomPrice($calculatedPrice);
            $item->save();

        }

        $quote->collectTotals()->save();

    }


Hope this implementation will help someone with the same problem.

-1

You can use sales_order_place_before Observer for changing price.

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
    <Observersample_Event>
        <version>0.0.1</version>
    </Observersample_Event>
</modules>
<global>
    <models>
        <Observersample_Event>
            <class>Observersample_Event_Model</class>
        </Observersample_Event>
    </models>
    <events>
        <sales_order_place_before>
            <observers>
                <observersample_event_model_observer>
                    <type>singleton</type>
                    <class>Observersample_Event_Model_Observer</class>
                    <method>example</method>
                </observersample_event_model_observer>
            </observers>
        </sales_order_place_before>
    </events>
</global>
</config>

observer.php

class Observersample_Event_Model_Observer{

  public function example($observer){
    $quote = $observer->getEvent()->getQuote();
    foreach ($quote->getAllItems() as $item) {
       $product = $item->getProduct();//if you need it
       $product->setOriginalCustomPrice(<YourPrice>);
       $product->save();
      //your magic here.
    }
    $quote->save();
    Mage::log("Order is placed");
  }
}
7
  • I am using the same approach that you mentioned, also I update my question with the code I am using for custom checkout, but its not working for me. Can you please check what I am doing wrong ? Jan 6, 2017 at 7:04
  • use setOriginalCustomPrice() instead of setPrice(), check my updated answer. Jan 6, 2017 at 7:28
  • I changed by observer to this class CustomCheckout_Event_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->getProduct()->setIsSuperMode(true); //your magic here. } } } But its still not working. Jan 6, 2017 at 8:54
  • add $item->save() in foreach loop and $quote->save() after. Jan 6, 2017 at 9:00
  • I tried that too, but its not working. Jan 6, 2017 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.