下订单后,将订单信息导出到.txt文件

时间:2013-12-11 08:46:19

标签: php magento magento-1.7

我已经研究并尝试了所有可能的想法,但我没有任何运气,但是我错过了什么?

想法是在我的Magento商店下订单后导出文本文件。然后,文本文件将用于打印机自动打印订单。

我在Magento中创建了一个自定义模块,用于查看" sales_order_place_after" (请告知这是否是错误的事件)并执行Observer.php代码。

我在config.xml文件中有这个:

    <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>

这是我的Observer.php文件:

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!!!!!!!!!!!!!!!!');
    }
}

当我下订单时,输出只是&#34; 1&#34;。我需要获取所有导出的订单信息,以便打印完整的订单。

系统日志显示:

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

创建文件;内容仅显示:

content here....1

我错过了什么或做错了什么?(仍在学习......)任何建议都会非常感激。

由于

4 个答案:

答案 0 :(得分:0)

代码中的错误是

$observerdata = print_r($observerdata);

print_r('blah')blah输出到stdout并在成功时返回true。此布尔值将转换为字符串,从而导致在脚本中显示1

只需将此行更改为

即可
$observerdata = print_r($observerdata, true);

第二个参数告诉print_r返回值而不是打印它。

答案 1 :(得分:0)

尝试更改

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

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

$ observer-&gt; getOrder()足以获得此活动的订单

答案 2 :(得分:0)

我的代码实际上存在一些问题;最终结果如下所示,现在正在导出基本数据。仍在制定产品的foreach循环,因为此时只导出最后一项;但这超出了这个问题的范围。

感谢您的帮助。

config.xml中

    <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>

Observer.php

class Company_PrinterOrder_Model_Observer 
{

    public function exportOrder(Varien_Event_Observer $observer)
    {
    Mage::log('!!!!!!!!!!!!!!!!First line of the Observer!!!!!!!!!!!!!!!!');

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

    //Define product items
    $total_items = $order->getTotalItemCount();
    $items = $order->getAllItems();

    if ($items) {
        foreach ($items as $item) {
        $item_info = ' Qty: '.$item['qty_ordered'].' Item:'.$item['name'].' Price: '.$item['price'];
        }
    }



    //CREATE THE TXT FILE
        // choose path
        $path = $_SERVER['DOCUMENT_ROOT'] . '/magento/printer/orders/';
        if (!is_dir($path)) {

            mkdir($path);
        }

        // format order.txt
        $filename = 'order.txt';

        // create content
        $content =
        'OrderID:' . print_r($order->getIncrementId(), true) . 
        ' AnotherID:' . print_r($order->getId(), true) . 
        ' RealOrderId:' . print_r($order->getRealOrderId(), true) . 
        ' DeliveryMethod:' . print_r($order->getShippingMethod(), true) . 
        ' DeliveryAddress:' . print_r($order->getShippingAddress()->getData("street"), true) . ', ' . print_r($order->getShippingAddress()->getData("city"), true) . 
        ' Subtotal:' . print_r($order->getSubtotal(), true) . 
        ' DeliveryCharge:' . print_r($order->getShippingAmount(), true) . 
        ' TotalPaid:' . print_r($order->getTotalPaid(), true) . 
        ' TotalDue:' . print_r($order->getTotalDue(), true) . 
        ' CustomerNote:' . print_r($order->getCustomerNote(), true) . 
        ' PhoneNumber:' . print_r($order->getShippingAddress()->getTelephone(), true) . 
        ' Email:' . print_r($order->getCustomerEmail(), true) . 
        ' CustomerName:' . print_r($order->getCustomerName(), true);


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


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

答案 3 :(得分:0)

尝试以下链接:

此链接用于在下订单后将订单信息导出为.txt / .csv文件格式。

https://github.com/vijays91/Magento-Placed-Order-Export