3

I am developing a custom module where an xml file will be created after order completion for each order.

I have called a function exportOrder on occuring of an event "sales_order_place_after". Following is the code of my function

public function exportOrder($order){
    $dirPath = Mage::getBaseDir('var').DS.'export';

    //if the export directory does not exist, create it
    if (!is_dir($dirPath)) {
         mkdir($dirPath, 0777, true);
    }

    $data = $order->getData();

    $xml = new SimpleXMLElement('<root/>');
    array_walk_recursive($data, array ($xml, 'addChild'));

    file_put_contents(
        $dirPath.DS.$order->getIncrementId().'.xml',
        $xml->asXML()
    );

Now here on line no 12 ie: $data->getData(); I am getting an error

Fatal error: Call to a member function getData() on a non-object in /home/user_www/aliasgar/magentotesting/app/code/local/Meteorify/Observerexample/Model/Export.php on line 12

My config.xml is as follows

<global>
<helpers>
    <orderexport>
        <class>Meteorify_Observerexample_Helper</class>
    </orderexport>
</helpers>
<models>
    <meteorify_observerexample>
         <class>Meteorify_Observerexample_Model</class>
    </meteorify_observerexample>
</models>
<events>
  <checkout_onepage_controller_success_action>
    <observers>
      <sales_order_place_after>
        <type>singleton</type>
        <class>Meteorify_Observerexample_Model_Observer</class>
        <method>exportOrder</method>
      </sales_order_place_after>
    </observers>
  </checkout_onepage_controller_success_action>     
</events>

This is how my Observer.php file looks

class Meteorify_Observerexample_Model_Observer extends Varien_Object{

public function exportOrder($observer) {
    $order = $observer->getEvent()->getOrder();
    Mage::log($order);
    Mage::getModel('meteorify_observerexample/export')->exportOrder($order);
    return true;
}

}

I am not able to figure out the mistake. Please help me in this

3

You are using the event incorrectly.

In event checkout_onepage_controller_success_action

the order is not sent as an object, but as the order id. (as order_ids)

Mage::dispatchEvent('checkout_onepage_controller_success_action', array('order_ids' => array($lastOrderId)));

(ref Mage_Checkout_OnepageController::successAction)

Thus in your observer you need to get the id, as such:

$orderIds = $observer->getEvent()->getOrderIds(); // returns an array 
$orderId = array_shift($orderIds);

and instantiate the order object using

$order = mage::getModel('sales/order')->load($orderId);

(the above written off the top of my head, but the jest of it should be correct)

| improve this answer | |
  • Thank you for your suggestion. Now i am getting the value of orderId using $orderId = $observer->getEvent()->getOrderIds(); and placed $order = mage::getModel('sales/order')->getId($orderId);' but still the problem prevails. The error i am getting is Fatal error: Call to a member function getData() on a non-object in /home/user_www/aliasgar/magentotesting/app/code/local/Meteorify/Observerexample/Model/Export.php on line 11 I am not getting is there any mistake in xml file? – Dexter Aug 12 '13 at 13:36
  • Also $order = Mage::getModel('sales/order')->getId($orderId); using var_dump gives me NULL value for this line of code – Dexter Aug 12 '13 at 13:39
  • note the edit in my answer - use ->load($orderId) and not getId() - a typo/mistake ;) – ProxiBlue Aug 12 '13 at 14:43
  • also note further edits. Note that the value of order_ids is an array of values, thus can receive multiple order ids. You'll need to code to accomodate for such an instance. The code given shoudl get you on the right track, and is untested. – ProxiBlue Aug 12 '13 at 14:54
  • also, in the future, if you use an observer/event, and you get stuck, the simplest way forward is to grep the core code for code that uses the same event. Thus first grep for the event name, which will yield the config.xml where it is defined, and from that you will get the model/method. Then you can peek at the core code to see what is done in the event. You can also grep for where the event is fired, to see what values are passed. – ProxiBlue Aug 12 '13 at 14:58
3

In your observer you don't get the $order directly, you need to get it from the observer parameter like this:

public function exportOrder($observer){
    $order = $observer->getEvent()->getOrder();
    $data = $order->getData();
| improve this answer | |
  • @Vladmir Kerkhoff: In my function exportOrder($order), in the file Export.php here $order as passed in exportOrder from Observer.php is $order = $observer->getEvent()->getOrder();. Is it necessary to redeclare it ? – Dexter Aug 12 '13 at 12:45
  • Check my file Observer.php public function exportOrder(Varien_Event_Observer $observer) { $order = $observer->getEvent()->getOrder(); Mage::getModel('meteorify_observerexample/export')->exportOrder($order); return true; } – Dexter Aug 12 '13 at 12:45
  • No, this should work then. You can try to do a Mage::log($order) in the observer file to check what data you receive. – Vladimir Kerkhoff Aug 12 '13 at 12:50
  • I'll try out the log then – Dexter Aug 12 '13 at 12:58
  • This is what i am getting in log 2013-08-12T12:57:16+00:00 DEBUG (7): and using var_dump i am getting NULL value. – Dexter Aug 12 '13 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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