我想知道,我是否可以为magento(1.7或1.9)上的每个订单自动创建/导出csv文件
我需要它们包含调度订单,产品,所选属性,客户详细信息等所需的所有信息。我需要以类似'order_number_timestamp.csv'的格式命名的文件
也许用一些php和一个cron脚本可以实现它?
由于
答案 0 :(得分:0)
Leeboyce,您可以使用Magento Event Observer functionality
执行此操作。
当触发一个将要运行的事件时,将触发一个在Order place
上的函数创建扩展程序将使用csv发送邮件
步骤:在app/code/local/Amit/Sendorderdata/etc/
下创建config.xml
config.xml的代码是
<?xml version="1.0" encoding="utf-8"?>
<config>
<modules>
<Amit_Sendorderdata>
<version>0.1.4</version>
</Bh_Sendorderdata>
</modules>
<global>
<models>
<sendorderdata>
<class>Amit_Sendorderdata_Model</class>
</sendorderdata>
</models>
</global>
<frontend>
<events>
<checkout_submit_all_after>
<observers>
<sendorderdata_observer_checkout_type_onepage_save_order_after>
<type>singleton</type>
<class>sendorderdata/observer</class>
<method>checkoutTypeOnepageSaveOrderAfter</method>
</sendorderdata_observer_checkout_type_onepage_save_order_after>
</observers>
</checkout_submit_all_after>
</events>
</frontend>
</config>
第2步:创建Observer.php app/code/local/Amit/Sendorderdata/Model/
在此文件中有一个函数checkoutTypeOnepageSaveOrderAfter which send mail
...
<?php
class Amit_Sendorderdata_Model_Observer
{
public function checkoutTypeOnepageSaveOrderAfter($observer)
{
$order = $observer->getEvent()->getOrder();
if (!$order) {
$orders = $observer->getEvent()->getOrders();
$order = array_shift($orders);
}
/* Here you will write yours code for create $order will give all detail of order */
}
如何在magento here
中创建csv了解有关事件和观察员的更多信息
http://inchoo.net/category/ecommerce/magento/events-observers/
http://www.pierrefay.com/event-observers-magento-tutorial-howto-105
http://www.nicksays.co.uk/magento-events-cheat-sheet-1-7/
答案 1 :(得分:0)
您可以使用Magento Event Observer功能执行此操作,如下所述: 步骤1:转到app / code / local / Allin / CustomModule / etc /下的config.xml,并在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.php创建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);
}
}
}
?>