Magento 1.7 – Export Order Info to .txt File Once Order is Placed

event-observermagento-1.7modulePHPxml

I have researched and tried all possible ideas I could come up with but not having any luck with the below…what am I missing?

The idea is to export a text file once an order has been placed on my Magento store. The text file will then be used for the printer to print the order automatically.

I have created a custom module in Magento which looks at the "sales_order_place_after" (Please advise if this is the wrong event to look at for this) event and executes the Observer.php code.

I have this in config.xml file:

    <events>
        <sales_order_place_after>
            <observers>
                <company_printerorder>
                    <type>singleton</type>
                    <class>company_printerorder/observer</class>
                    <method>exportOrder</method>
                </company_printerorder>
            </observers>
        </sales_order_place_after>
    </events>

This is my Observer.php file:

class Company_PrinterOrder_Model_Observer 
{

    public function exportOrder(Varien_Event_Observer $observer)
    {

    Mage::log('!!!!!!!!!!!!!!!!First line of the Observer!!!!!!!!!!!!!!!!');

    //**** I THINK MY PROBLEM IS HERE
    $event = $observer->getEvent()->getOrder()->getData();
    $observerdata = (get_class_methods($event));
    $observerdata = print_r($observerdata); 




    //Create text file
        // choose correct path
        $path = $_SERVER['DOCUMENT_ROOT'] . '/magento/printer/orders/';
        if (!is_dir($path)) {

            mkdir($path);
        }

        // create filename
        $orderId = Mage::getSingleton('checkout/type_onepage')
            ->getCheckout()->getLastOrderId();
        $orderoverall = Mage::getModel('sales/order')->load($orderId);
        $orderNo = $orderoverall->getIncrementId();

        // format 20090403_141048_order_100000007_7.xml
        $filename = date('Ymd_His') . '_order_' . $orderNo . '_' . $orderId . '.txt';

        // create content
        $content = 'content here....' . $observerdata; // create TXT Data here

        // write file to server
        file_put_contents($path . $filename, $content);


    Mage::log('!!!!!!!!!!!!!!!!File has been created!!!!!!!!!!!!!!!!');

    Mage::log($observerdata);

    Mage::log('!!!!!!!!!!!!!!!!Just made a successful Observer!!!!!!!!!!!!!!!!');
    }
}

When I place an order the output is simply just "1". i need to get all the order info exported so that the complete order can print out.

The system log shows:

2013-12-11T06:29:18+00:00 ERR (3): Notice: Array to string conversion  in C:\xampp\htdocs\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 2814
2013-12-11T06:29:18+00:00 ERR (3): Notice: Array to string conversion  in C:\xampp\htdocs\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 2814
2013-12-11T06:29:19+00:00 ERR (3): Notice: Array to string conversion  in C:\xampp\htdocs\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 2814
2013-12-11T06:29:19+00:00 ERR (3): Notice: Array to string conversion  in C:\xampp\htdocs\magento\lib\Varien\Db\Adapter\Pdo\Mysql.php on line 2814
2013-12-11T06:29:21+00:00 DEBUG (7): !!!!!!!!!!!!!!!!First line of the Observer!!!!!!!!!!!!!!!!
2013-12-11T06:29:21+00:00 DEBUG (7): !!!!!!!!!!!!!!!!File has been created!!!!!!!!!!!!!!!!
2013-12-11T06:29:21+00:00 DEBUG (7): 1
2013-12-11T06:29:21+00:00 DEBUG (7): !!!!!!!!!!!!!!!!Just made a successful Observer!!!!!!!!!!!!!!!!
2013-12-11T06:29:22+00:00 DEBUG (7): SMTPPro is enabled, sending email in Aschroder_SMTPPro_Model_Email_Template
2013-12-11T06:29:22+00:00 DEBUG (7): Development mode set to send all emails to contact form recipient: email@gmail.com
2013-12-11T06:29:23+00:00 DEBUG (7): ReplyToStoreEmail is enabled, just set Reply-To header: email@gmail.com
2013-12-11T06:29:23+00:00 DEBUG (7): Preparing the Google Apps/Gmail Email transport, email to send with is: email@gmail.com
2013-12-11T06:29:23+00:00 DEBUG (7): Returning transport
2013-12-11T06:29:23+00:00 DEBUG (7): About to send email
2013-12-11T06:29:29+00:00 DEBUG (7): Finished sending email
2013-12-11T06:29:29+00:00 DEBUG (7): template=sales_email_order_template

The file gets created; the content only shows:

content here....1

What am I missing or doing wrong?(still learning…) Any advise would be much appreciated.

Thanks

Best Answer

Here is what's wrong.
First of all:

$event = $observer->getEvent()->getOrder()->getData();

This will get you an array.
and when calling later

$observerdata = (get_class_methods($event));

You get nothing because $event is an array.
Then there is this:

$observerdata = print_r($observerdata); 

After this $observerdata will be 1 because that's what print_r returns. Tip for the future: you can pass a second parameter to print_r to get it to return the value instead of printing it. print_r($something, true).

Next thing.
You don't need t load the order again:

$orderId = Mage::getSingleton('checkout/type_onepage')
            ->getCheckout()->getLastOrderId();
$orderoverall = Mage::getModel('sales/order')->load($orderId);

The order is already passed to the observer. Get it like this:

$order = $observer->getEvent()->getOrder();

Now to get all the data replace:

$content = 'content here....' . $observerdata;

With

$content = 'content here....' . print_r($order->getData(), true);
Related Topic