1

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

2

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);
  • Thank you for the informative response Marius. I just made the changes as you described, but now when pressing "Place Order" it just keeps loading. When I look at the system log only this shows "2013-12-11T11:05:48+00:00 DEBUG (7): !!!!!!!!!!!!!!!!First line of the Observer!!!!!!!!!!!!!!!!" Meaning the rest of the code didn't work. There was also no file created this time. Is there perhaps something else I am missing? – Christo Dec 11 '13 at 11:05
  • @Christo. Yeah. maybe it's not a good idea to log ` print_r($order->getData(), true);` It can be very big. Maybe you should try to log only what you need. Try for starters to log only $order->getIncrementId(). – Marius Dec 11 '13 at 12:05
  • Excellent it works with "getIncrementId". I just tried some additional ones; tried "getProductId", "getShippingMethod", "getShippingAddress" but all 3 of them just produce the same result as my previous comment. Am I using incorrect methods there? – Christo Dec 11 '13 at 13:34
  • Actually got that working thanks. Just had to be more specific with the data I want. Instead of just using "$order->getShippingAddress()" I had to use "$order->getShippingAddress()->getData("street")". Thanks for the assitance @Marius – Christo Dec 12 '13 at 5:23

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.