1

I need to do relatively a small thing with magento. But confused since I am a beginner in module development.

  • I added 2 columns named percentorder and commission into table sales_flat_order
  • Now I need to add values to those two columns when user creates an Order.

How can I do that? Should I override the order related models? or write an event Observer?

1 Answer 1

5

You can do this easily with an Observer.

Firstly, I would add this to your config.xml:

<config>
...
  <global>
  ...
    <events>
      <sales_order_save_before>
        <observers>
          <module_order_save_before>
            <type>singleton</type>
            <class>module/observer</class>
            <method>orderSaveBefore</method>
          </module_order_save_before>
        </observers>
      </sales_order_save_before>
    </events>
  ...
  </global>
...
</config>

If you are calculating the values of these 2 columns, you can do the following in your Observer.

Observer.php:

public function orderSaveBefore($obs) {
  $event = $obs->getEvent();
  $order = $event->getOrder();

  $order->setPercentorder(value);
  $order->setCommission(value);
  return $this;
}

If however, you're relying on input from a form that you can attach to with an event, you can do something similar to this:

Observer.php

public function orderSaveBefore($obs) {
  $event = $obs->getEvent();
  $order = $event->getOrder();
  $request = Mage::app()->getFrontController()->getRequest();
  $percentorder = $request->getParam('percentorder');
  $commission = $request->getParam('commission');

  $order->setPercentorder(value);
  $order->setCommission(value);
  return $this;
}

If you hook into the models save_before event, you can set the values for the columns before the order actually gets saved, reducing the cost of having to use save() again.

3
  • Glad to help :) Commented Aug 3, 2013 at 20:55
  • @Daniel how to save it to sales_flat_order as well? I tried sales_order_save_before with $event->getQuote(); but it is not working
    – Anto S
    Commented Nov 1, 2015 at 15:10
  • @anto.nishanth - If you're trying to fetch the order, you would want $event->getOrder(), not $event->getQuote(). Try that and let me know if it works! Commented Nov 3, 2015 at 19:21

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.