MJQ
MJQ

Reputation: 1786

Adding a comment to order in magento

I am working on magento. I want add a functionality that when user places the order, a comment is added to the history comment of the order. I have gone through the code and come to know that the function

 public function addStatusHistoryComment($comment, $status = false)

in order.php is used to add the comment. I want to access it when user places the order. So how can i do that? Do anyone have any idea?

Upvotes: 3

Views: 5606

Answers (1)

Theodores
Theodores

Reputation: 1209

As with anything in Magento there are many ways.

First you need to write a module. In that module you could listen for checkout success event - checkout_onepage_controller_success_action. Do that with the module etc/config.xml, e.g:

    <events>
        <checkout_onepage_controller_success_action>
            <observers>
                <whatever>
                    <type>singleton</type>
                    <class>whatever/observer</class>
                    <method>checkout_onepage_controller_success_action</method>
                </whatever>
            </observers>
        </checkout_onepage_controller_success_action>
    </events>

In your observer you load the last order, append your comment to it and then you save your order. The method you describe will work perfectly. You can also do things with the order status, doing so enables you to email the customer if need be:

public function checkout_onepage_controller_success_action($observer) {
    $orderIds=$observer->getData('order_ids');
    foreach ($orderIds as $orderId) {
        $order = new Mage_Sales_Model_Order();
        $order->load($orderId);

        ... Do Something!

        $order->setState('processing', 'invoiced', 'Hello World!');
        $order->save();
    }

I hope that helps!

Upvotes: 4

Related Questions