Magento:为每个订单自动创建CSV文件 [英] Magento: Automatically create CSV File for each order placed

查看:256
本文介绍了Magento:为每个订单自动创建CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,是否有可能为放置在magento(1.7或1.9)上的每个订单自动创建/导出csv文件

I am wondering, is it possible for me to automatically create/export a csv file for each order that is placed on magento (1.7 or 1.9)

需要它们包含分发订单,产品,所选属性,客户详细信息等的所有必需的信息。我需要以类似于'order_number_timestamp.csv'的格式命名的文件

I would need them to contain all the required info for dispatching an order, products, selected attributes, customer details etc. And i would need the files named in a format similar to 'order_number_timestamp.csv'

也许可以用一些php和cron脚本实现?

Perhaps it is achievable with some php and a cron script?

谢谢

推荐答案

您可以使用Magento Event Observer功能,如下所示:
步骤1:转到app / code / local / Allin / CustomModule / etc /下的config.xml并添加以下代码config.xml

You can do this using Magento Event Observer functionality as stated below: Step1: go to config.xml under app/code/local/Allin/CustomModule/etc/ and add the following code in config.xml

config.xml
......
    <frontend>
                <events>
                        <sales_order_save_after>
                                <observers>
                                        <generate_csv_save_order_after>
                                                <type>singleton</type>
                                                <class>custommodule/observer</class>
                                                <method>customCSV</method>
                                        </generate_csv_save_order_after>
                                </observers>
                        </sales_order_save_after>
                </events>
  </frontend>

步骤2:在app / code / local / Allin / CustomModule / Model / Observer中创建Observer.php。 php

Step2: Create Observer.php at app/code/local/Allin/CustomModule/Model/Observer.php

<?php

class Allin_CustomModule_Model_Observer
{
    public function customCSV($observer)
    {
                $id = $observer->getEvent()->getOrder()->getId();
                $order = Mage::getModel('sales/order')->load($id);
                $orderIncrementId = $order->getIncrementId();
                $billingAddress = $order->getBillingAddress()->getData();
                $billingAdd=array(
                'Billing Name' => $billingAddress['firstname']."
".$billingAddress['lastname'],
                'Billing Company' => $billingAddress['company'],
                'Billing Street' => $billingAddress['street'],
                'Billing Zip' => $billingAddress['postcode'],
                'Billing City' => $billingAddress['city'],
                'Billing State' => $billingAddress['region'],
                'Billing Country' => $billingAddress['country_id'],
                'Billing Phone' => $billingAddress['telephone'],
                );

                $orderData = Mage::getResourceModel('sales/order_collection')
                        ->addFieldToSelect(array('entity_id'=>'entity_id',
                        'Order Number'=>'increment_id',
                        'Order Date'=>'created_at',
                        'Order Status'=>'status',
                        'Order Purchased From'=>'store_name',
                        'Order Shipping Method'=>'shipping_method',
                        'Order Subtotal'=>'subtotal',
                        'Order Tax'=>'tax_amount',
                        'Order Shipping'=>'shipping_amount',
                        'Order Discount'=>'discount_amount',
                        'Order Grand Total'=>'grand_total',
                        'Order Base Grand Total'=>'base_grand_total',
                        'Order Paid'=>'total_paid',
                        'Order Refunded'=>'total_refunded',
                        'Order Due'=>'total_due',
                        'Total Qty Items Ordered'=>'total_qty_ordered',
                        'Customer Email'=>'customer_email',
                        'Stock Health Customer ID'=>'customer_id',
                        'Stock Health Customer ID'=>'customer_id',

                        ))->addExpressionFieldToSelect(
               'Customer Name',
               'CONCAT({{customer_firstname}}, \' \',
{{customer_lastname}})',
               array('customer_firstname' =>
'main_table.customer_firstname', 'customer_lastname' =>
'main_table.customer_lastname')
                           )->addExpressionFieldToSelect(
               'Shipping Name',
               'CONCAT({{firstname}}, \' \', {{lastname}})',
               array('firstname' => 'sales_flat_order_address.firstname',
'lastname' => 'sales_flat_order_address.lastname')
                           )
                           ->join(array('sales_flat_order_address' => 'sales/order_address'),
                   "main_table.entity_id = sales_flat_order_address.parent_id AND
sales_flat_order_address.address_type != 'billing'
                       AND main_table.increment_id='$orderIncrementId'  ",
                   array(
                                'Shipping Company'       => 'company',
                                'Shipping Street'     => 'street',
                                'Shipping Zip'     => 'postcode',
               'Shipping City'       => 'city',
                           'Shipping State'       => 'region',
               'Shipping Country' => 'country_id',
               'Shipping Phone Number'  => 'telephone',

           ))->join(array('sales_flat_order_payment' =>
'sales/order_payment'), 'main_table.entity_id =
sales_flat_order_payment.parent_id',
                   array(
               'Order Payment Method' => 'method',
                           'Credit Card Type' => 'cc_type'
           ))->join(array('sales_flat_order_item' => 'sales/order_item'),
'main_table.entity_id = sales_flat_order_item.order_id',
                   array(
                                'Order Item Increment' => 'item_id',
               'Item Name' => 'name',
                           'sku' => 'sku',
                           'Item Options' => 'product_options',
                           'Item Original Price' => 'original_price',
                           'Item Price' => 'price',
                           'Item Qty Ordered' => 'qty_ordered',
                           'Item Qty invoiced' => 'qty_invoiced',
                           'Item Qty shipped' => 'qty_shipped',
                           'Item Qty Canceled' => 'qty_canceled',
                           'Item Qty Refunded' => 'qty_refunded',
                           'Item Tax' => 'tax_amount',
                           'Item Discount' => 'discount_amount',
                           'Item Total' => 'row_total_incl_tax',
           ));

                $headings=$orderData->getData();

                foreach($headings as $k => $v)
                {
                        array_shift($v);
                        $heading[]= array_merge($v,$billingAdd);
                }

                $filename = 'csvorder/'.date('dmY-His').$orderIncrementId.".csv";

                foreach($heading[0] as $key => $value ) {
                        $labelArray .= $key.",";
                }

                $labelArray = substr($labelArray, 0, -1);
                $valueArray = substr($valueArray, 0, -1);
                $data[] = explode(",", $labelArray);

                foreach($heading as $key => $value ) {
                        $data[] = $value;
                }
                if(count($data) > 0 ) {
                        $fp = fopen($filename, 'w');
                        foreach ($data as $product) {
                                fputcsv($fp, $product);
                        }
                        fclose($fp);
                }
        }

}

?>

这篇关于Magento:为每个订单自动创建CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆