1

I am trying to get data from a new order placed on our website to use it for integration with our ERP/CRM system.

I use the event sales_order_place_after and my observer function fires as expected, but it seems the order object passed to the observer has no data at all. I also tried the event checkout_submit_all_after as pointed in this answer with same results.

After placing a new order I can see it normally in the admin panel, with all the items and customer data, so maybe I am not accessing it correctly in the observer.

My observer function looks like this:

public function newOrder(Varien_Event_Observer $observer)
{
    Mage::log(print_r('new order function start', true), null, 'new-order.log');

    // tried both those
    $order = $observer->getEvent()->getData('order');
    $order = $observer->getEvent()->getOrder(); // tried this too

    // tried both those
    $items = $order->getAllVisibleItems();
    $items = $order->getAllItems();

    $customer_id = $order->getCustomerId();
    $customer_name = $order->getCustomerName();
    $customer_email = $order->getCustomerEmail();

    // Those had same result
    //$customer_id = $order->getCustomer()->getId();
    //$customer_name = $order->getCustomer()->getName();
    //$customer_email = $order->getCustomer()->getEmail();

    Mage::log(print_r($items, true), null, 'new-order.log');
    Mage::log(print_r($customer_id, true), null, 'new-order.log');
    Mage::log(print_r($customer_name, true), null, 'new-order.log');
    Mage::log(print_r($customer_email, true), null, 'new-order.log');

    Mage::log(print_r('new order function end', true), null, 'new-order.log');
}

And the output on the log file:

2017-05-18T11:10:10+00:00 DEBUG (7): new order function start

The end function log never appears. Am I accessing the data in a wrong way?

2

The object is not empty, it is just to large to write it to logfile. This breaks the observer method and your last Mage::log('function end') is not reached.

For debugging of large objects you should use debug() the that works with every type of Varien Object. Advantage over var_dump or print_r is that it will leave out all recursive data and keeps your log small.

/**
 * Present object data as string in debug mode
 *
 * @param mixed $data
 * @param array $objects
 * @return string
 */
public function debug($data=null, &$objects=array())
{
    if (is_null($data)) {
        $hash = spl_object_hash($this);
        if (!empty($objects[$hash])) {
            return '*** RECURSION ***';
        }
        $objects[$hash] = true;
        $data = $this->getData();
    }
    ...
}

Try this instead ...

public function newOrder(Varien_Event_Observer $observer)
{
    $order = $observer->getOrder();
    Mage::log($items->debug(), null, 'new-order.log');

    # only get available data keys
    Mage::log(array_keys($order->getData()), null, 'new-order.log');
}
  • It was way more than just useful. I did upvote but it does not show, since I am below 15 rep on this site. – AP Datum May 19 '17 at 8:50

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.