magento导出order,生成xml格式

xml格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?xml version="1.0"?>
<Orders version="1.0">
<Order>
<CompanyIdentifier></CompanyIdentifier>
<OrderIdentifier>300000001</OrderIdentifier>
<OrderType>1</OrderType>
<Status>pending</Status>
<OrderDate Format="YYYYMMDD">20140911</OrderDate>
<OrderTotalAmount>26.9000</OrderTotalAmount>
<VatTotalAmount>5.2100</VatTotalAmount>
<OrderTotalVatExcludedAmount>21.6900</OrderTotalVatExcludedAmount>
<OrderInformation>
<DeliveryDate Format="YYYYMMDD">20140911</DeliveryDate>
<OrderFreeText></OrderFreeText>
<OrderLanguage>FIN</OrderLanguage>
<CustomerOrderNo>300000001</CustomerOrderNo>
<OurReference>300000001</OurReference>
<YourReference></YourReference>
<DeliveryMethodText>freeshipping_freeshipping</DeliveryMethodText>
<DeliveryTermsText>Sopimuksen mukaan</DeliveryTermsText>
<SellerContactName>Verkkokauppa</SellerContactName>
<SellerContactNo>5000001</SellerContactNo>
<OrderAccount></OrderAccount>
</OrderInformation>
<PaymentTerms>
<PaymentTermsText>Maksettu</PaymentTermsText>
<PaymentTermsNetDates>0</PaymentTermsNetDates>
<CashDiscountDates>0</CashDiscountDates>
<CashDiscountPercent>0</CashDiscountPercent>
</PaymentTerms>
<InvoiceCustomer>
<ExternalCustomerIdentifier>VK300000001</ExternalCustomerIdentifier>
<CustomerName1>zou hongzhao</CustomerName1>
<CustomerName2></CustomerName2>
<CustomerAddress><0>wrasr</0></CustomerAddress>
<CustomerPostCode>430000</CustomerPostCode>
<CustomerTownName>weqrwer</CustomerTownName>
<CustomerCountryCode>FI</CustomerCountryCode>
<CustomerCountryName></CustomerCountryName>
<CustomerPhone2>134234234234</CustomerPhone2>
<CustomerFax></CustomerFax>
<CustomerEmail>fenzsofttest2@126.com</CustomerEmail>
<CustomerTaxCode></CustomerTaxCode>
<CustomerIdentifier>VK300000001</CustomerIdentifier>
<CustomerFinvoiceAddress></CustomerFinvoiceAddress>
<CustomerFinvoiceIntermediator></CustomerFinvoiceIntermediator>
</InvoiceCustomer>
<OrderRows>
<OrderRow>
<RowArticleCode>HS1326-10</RowArticleCode>
<RowArticle>HS1326</RowArticle>
<RowArticleInformation></RowArticleInformation>
<RowOrderedQuantity>1.0000</RowOrderedQuantity>
<RowQuantityUnitCode></RowQuantityUnitCode>
<RowUnitPrice>21.6900</RowUnitPrice>
<RowDiscountPercent>0.0000</RowDiscountPercent>
<RowVatPercent>24.0000</RowVatPercent>
<RowAccount></RowAccount>
<RowAccountDimension></RowAccountDimension>
<RowVatAmount>5.2100</RowVatAmount>
<RowAmount>26.9000</RowAmount>
<RowVatExcludedAmount>21.6900</RowVatExcludedAmount>
<RowWarehouseCode>A1</RowWarehouseCode>
</OrderRow>
</OrderRows>
</Order>
<Order>
<CompanyIdentifier></CompanyIdentifier>
<OrderIdentifier>300000002</OrderIdentifier>
<OrderType>1</OrderType>
<Status>pending</Status>
<OrderDate Format="YYYYMMDD">20140911</OrderDate>
<OrderTotalAmount>49.8000</OrderTotalAmount>
<VatTotalAmount>9.6400</VatTotalAmount>
<OrderTotalVatExcludedAmount>40.1600</OrderTotalVatExcludedAmount>
<OrderInformation>
<DeliveryDate Format="YYYYMMDD">20140911</DeliveryDate>
<OrderFreeText></OrderFreeText>
<OrderLanguage>FIN</OrderLanguage>
<CustomerOrderNo>300000002</CustomerOrderNo>
<OurReference>300000002</OurReference>
<YourReference></YourReference>
<DeliveryMethodText>freeshipping_freeshipping</DeliveryMethodText>
<DeliveryTermsText>Sopimuksen mukaan</DeliveryTermsText>
<SellerContactName>Verkkokauppa</SellerContactName>
<SellerContactNo>5000001</SellerContactNo>
<OrderAccount></OrderAccount>
</OrderInformation>
<PaymentTerms>
<PaymentTermsText>Maksettu</PaymentTermsText>
<PaymentTermsNetDates>0</PaymentTermsNetDates>
<CashDiscountDates>0</CashDiscountDates>
<CashDiscountPercent>0</CashDiscountPercent>
</PaymentTerms>
<InvoiceCustomer>
<ExternalCustomerIdentifier>VK300000002</ExternalCustomerIdentifier>
<CustomerName1>zou hong</CustomerName1>
<CustomerName2></CustomerName2>
<CustomerAddress><0>qeqweqwe</0></CustomerAddress>
<CustomerPostCode>14321</CustomerPostCode>
<CustomerTownName>wuygha</CustomerTownName>
<CustomerCountryCode>FI</CustomerCountryCode>
<CustomerCountryName></CustomerCountryName>
<CustomerPhone2>14123432423</CustomerPhone2>
<CustomerFax></CustomerFax>
<CustomerEmail>zou@125.com</CustomerEmail>
<CustomerTaxCode></CustomerTaxCode>
<CustomerIdentifier>VK300000002</CustomerIdentifier>
<CustomerFinvoiceAddress></CustomerFinvoiceAddress>
<CustomerFinvoiceIntermediator></CustomerFinvoiceIntermediator>
</InvoiceCustomer>
<OrderRows>
<OrderRow>
<RowArticleCode>HS1325/Black-11</RowArticleCode>
<RowArticle>HS1325</RowArticle>
<RowArticleInformation></RowArticleInformation>
<RowOrderedQuantity>2.0000</RowOrderedQuantity>
<RowQuantityUnitCode></RowQuantityUnitCode>
<RowUnitPrice>20.0800</RowUnitPrice>
<RowDiscountPercent>0.0000</RowDiscountPercent>
<RowVatPercent>24.0000</RowVatPercent>
<RowAccount></RowAccount>
<RowAccountDimension></RowAccountDimension>
<RowVatAmount>9.6400</RowVatAmount>
<RowAmount>49.8000</RowAmount>
<RowVatExcludedAmount>40.1600</RowVatExcludedAmount>
<RowWarehouseCode>A1</RowWarehouseCode>
</OrderRow>
</OrderRows>
</Order>
</Orders>

 

2种方法:

1,DomDocument

cron.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public function exportOrders(){
$now = Mage::getModel('core/date')->date();
$start = Mage::getModel('core/date')->date('Y-m-d');
$orderCollention = Mage::getModel('sales/order')->getCollection();
$orderCollention->addAttributeToSelect('*');
$orderCollention->addAttributeToFilter('updated_at', array('from' => $start,'to'=>$now,'date'=>true));
$total = $orderCollention->getSize();
if($total > 0){
$dom=new DomDocument('1.0', 'utf-8');
// create Element
$orders = $dom->createElement('Orders');
$dom->appendchild($orders);
$order_version = $dom->createAttribute('version');
$orders->appendchild($order_version);
$aval = $dom->createTextNode('1.0');
$order_version->appendChild($aval);
$num = 0;

foreach ($orderCollention as $_order){
$storeModel = Mage::app()->getStore($_order->getStoreId());
$websiteModel = Mage::app()->getWebsite($storeModel->getWebsiteId());
$orderDetail = $_order->getData();
$num ++;
echo "order incrementId : {$_order->getIncrementId()}...({$num}/{$total})..\r\n ";

// attribute
$attribute_array = array(
'title' => array(
'size' => 1
)
);
$order_attribute_array = array(
'OrderDate' => array(
'Format' => 'YYYYMMDD'
),
'DeliveryDate' => array(
'Format' => 'YYYYMMDD'
),
);
//create order basic info
$billing_address = $_order->getBillingAddress();
$shipmentCollection = $_order->getShipmentsCollection()->getFirstItem();
$deliveryDate = '';
if($shipmentIncrementId = $shipmentCollection->getIncrementId()){
$shipment = Mage::getModel('sales/order_shipment')->load($shipmentIncrementId);
$deliveryDate = $shipment->getCreatedAt();
}

$order = $dom->createElement('Order');
$orders->appendchild($order);
$orderBasic = array(
'CompanyIdentifier'=>$billing_address->getVatId(),
'OrderIdentifier'=>$_order->getIncrementId(),
'OrderType'=>1,
'Status'=>$_order->getStatus(),
'OrderDate'=>Mage::getModel('core/date')->date('Ymd', strtotime($_order->getCreatedAt())),
'OrderTotalAmount'=>$_order->getGrandTotal(),
'VatTotalAmount'=>$_order->getTaxAmount(),
'OrderTotalVatExcludedAmount'=>$_order->getSubtotal(),
);
Mage::helper('hofler')->create_item($dom, $order, $orderBasic, $order_attribute_array);

//create order infomation
$orderInformation = $dom->createElement('OrderInformation');
$order->appendchild($orderInformation);
if($websiteModel->getCode() == 'b2b'){
$orderLanguage = 'FIN';
$paymentTermsText = '14 pv netto';
$paymentTermsNetDates = 14;
}elseif ($websiteModel->getCode() == 'b2b_se'){
$orderLanguage = 'SWE';
$paymentTermsText = '14 pv netto';
$paymentTermsNetDates = 14;
}else{
$orderLanguage = 'FIN';
$paymentTermsText = 'Maksettu';
$paymentTermsNetDates = 0;
}
$orderInfo = array(
'DeliveryDate'=>Mage::getModel('core/date')->date('Ymd', strtotime($deliveryDate)),
'OrderFreeText'=>'',
'OrderFreeText'=>'',
'OrderLanguage'=>$orderLanguage,
'CustomerOrderNo'=>$_order->getIncrementId(),
'OurReference'=>$_order->getIncrementId(),
'YourReference'=>'',
'DeliveryMethodText'=>$_order->getShippingMethod(),
'DeliveryTermsText'=>'Sopimuksen mukaan',
'SellerContactName'=>'Verkkokauppa',
'SellerContactNo'=>'5000001',
'OrderAccount'=>'',
);
Mage::helper('hofler')->create_item($dom, $orderInformation, $orderInfo, $order_attribute_array);

//create PaymentTerms
$paymentTerms = $dom->createElement('PaymentTerms');
$order->appendchild($paymentTerms);
$paymentInfo = array(
'PaymentTermsText'=>$paymentTermsText,
'PaymentTermsNetDates'=>$paymentTermsNetDates,
'CashDiscountDates'=>0,
'CashDiscountPercent'=>0
);
Mage::helper('hofler')->create_item($dom, $paymentTerms, $paymentInfo);

//create InvoiceCustomer
$invoiceCustomer = $dom->createElement('InvoiceCustomer');
$order->appendchild($invoiceCustomer);
if(is_numeric($_order->getCustomerId())){
$externalCustomerIdentifier = 'VK'.$_order->getCustomerId();
}else{
$externalCustomerIdentifier = 'VK'.$_order->getIncrementId();
}
if($websiteModel->getCode() == 'b2b'){
$customerFinvoiceAddress = $billing_address->getFinvoiceAddress();
$customerFinvoiceIntermediator = $billing_address->getFinvoiceIntermediator();
$customerIdentifier = $billing_address->getVatId();
}elseif ($websiteModel->getCode() == 'b2b_se'){
$customerFinvoiceAddress = $billing_address->getFinvoiceAddress();
$customerFinvoiceIntermediator = $billing_address->getFinvoiceIntermediator();
$customerIdentifier = $billing_address->getVatId();
}else{
$customerFinvoiceAddress = '';
$customerFinvoiceIntermediator = '';
$customerIdentifier = $externalCustomerIdentifier;
}
$invoiceInfo = array(
'ExternalCustomerIdentifier'=>$externalCustomerIdentifier,
'CustomerName1'=>$billing_address->getFirstname().' '.$billing_address->getMiddlename().' '.$billing_address->getLastname(),
'CustomerName2'=>'',
'CustomerAddress'=>$billing_address->getStreet(),
'CustomerPostCode'=>$billing_address->getPostcode(),
'CustomerTownName'=>$billing_address->getCity(),
'CustomerCountryCode'=>$billing_address->getCountryId(),
'CustomerCountryName'=>'',
'CustomerPhone2'=>$billing_address->getTelephone(),
'CustomerFax'=>$billing_address->getFax(),
'CustomerEmail'=>$billing_address->getEmail(),
'CustomerTaxCode'=>$billing_address->getVatId(),
'CustomerIdentifier'=>$customerIdentifier,
'CustomerFinvoiceAddress'=>'',
'CustomerFinvoiceIntermediator'=>''
);
Mage::helper('hofler')->create_item($dom, $invoiceCustomer, $invoiceInfo);

//create OrderRows
$orderRowsArray = array();
$items = $_order->getAllItems();
foreach ($items as $_item){
$parentId = $_item->getParentItemId();
$product_type = $_item->getProductType();
if(is_numeric($parentId) && $product_type == 'simple'){
continue;
}
array_push($orderRowsArray, array(
'RowArticleCode' => $_item->getSku(),
'RowArticle' =>$_item->getName(),
'RowArticleInformation' => '',
'RowOrderedQuantity' => $_item->getQtyOrdered(),
'RowQuantityUnitCode' => '',
'RowUnitPrice' => $_item->getPrice(),
'RowDiscountPercent' => $_item->getDiscountPercent(),
'RowVatPercent' => $_item->getTaxPercent(),
'RowAccount' => '',
'RowAccountDimension' => '',
'RowVatAmount' => $_item->getTaxAmount(),
'RowAmount' => $_item->getRowTotalInclTax(),
'RowVatExcludedAmount' =>$_item->getRowTotal(),
'RowWarehouseCode' => 'A1'
));
}
$orderRows = $dom->createElement('OrderRows');
$order->appendchild($orderRows);
foreach ($orderRowsArray as $data) {
$orderRow = $dom->createElement('OrderRow');
$orderRows->appendchild($orderRow);
Mage::helper('hofler')->create_item($dom, $orderRow, $data);
}

}
}

echo $dom->save($xmlName);
}

 

 

2,SimpleXMLElement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
public function exportOrders(){
$now = Mage::getModel('core/date')->date();
$start = Mage::getModel('core/date')->date('Y-m-d');
$orderCollention = Mage::getModel('sales/order')->getCollection();
$orderCollention->addAttributeToSelect('*');
$orderCollention->addAttributeToFilter('updated_at', array('from' => $start,'to'=>$now,'date'=>true));
$total = $orderCollention->getSize();
if($total > 0){
$num = 0;
// creating object of SimpleXMLElement
$xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><Orders version=\"1.0\"></Orders>");

foreach ($orderCollention as $_order){
$storeModel = Mage::app()->getStore($_order->getStoreId());
$websiteModel = Mage::app()->getWebsite($storeModel->getWebsiteId());
$orderDetail = $_order->getData();
$num ++;
echo "order incrementId : {$_order->getIncrementId()}...({$num}/{$total})..\r\n ";

//create OrderRows
$orderRowsArray = array();
$items = $_order->getAllItems();
foreach ($items as $_item){
$parentId = $_item->getParentItemId();
$product_type = $_item->getProductType();
if(is_numeric($parentId) && $product_type == 'simple'){
continue;
}
array_push($orderRowsArray, array(
'RowArticleCode' => $_item->getSku(),
'RowArticle' =>$_item->getName(),
'RowArticleInformation' => '',
'RowOrderedQuantity' => $_item->getQtyOrdered(),
'RowQuantityUnitCode' => '',
'RowUnitPrice' => $_item->getPrice(),
'RowDiscountPercent' => $_item->getDiscountPercent(),
'RowVatPercent' => $_item->getTaxPercent(),
'RowAccount' => '',
'RowAccountDimension' => '',
'RowVatAmount' => $_item->getTaxAmount(),
'RowAmount' => $_item->getRowTotalInclTax(),
'RowVatExcludedAmount' =>$_item->getRowTotal(),
'RowWarehouseCode' => 'A1'
));
}

$billing_address = $_order->getBillingAddress();
$shipmentCollection = $_order->getShipmentsCollection()->getFirstItem();
$deliveryDate = '';
if($shipmentIncrementId = $shipmentCollection->getIncrementId()){
$shipment = Mage::getModel('sales/order_shipment')->load($shipmentIncrementId);
$deliveryDate = $shipment->getCreatedAt();
}
if($websiteModel->getCode() == 'b2b'){
$orderLanguage = 'FIN';
$paymentTermsText = '14 pv netto';
$paymentTermsNetDates = 14;
}elseif ($websiteModel->getCode() == 'b2b_se'){
$orderLanguage = 'SWE';
$paymentTermsText = '14 pv netto';
$paymentTermsNetDates = 14;
}else{
$orderLanguage = 'FIN';
$paymentTermsText = 'Maksettu';
$paymentTermsNetDates = 0;
}
if(is_numeric($_order->getCustomerId())){
$externalCustomerIdentifier = 'VK'.$_order->getCustomerId();
}else{
$externalCustomerIdentifier = 'VK'.$_order->getIncrementId();
}
if($websiteModel->getCode() == 'b2b'){
$customerFinvoiceAddress = $billing_address->getFinvoiceAddress();
$customerFinvoiceIntermediator = $billing_address->getFinvoiceIntermediator();
$customerIdentifier = $billing_address->getVatId();
}elseif ($websiteModel->getCode() == 'b2b_se'){
$customerFinvoiceAddress = $billing_address->getFinvoiceAddress();
$customerFinvoiceIntermediator = $billing_address->getFinvoiceIntermediator();
$customerIdentifier = $billing_address->getVatId();
}else{
$customerFinvoiceAddress = '';
$customerFinvoiceIntermediator = '';
$customerIdentifier = $externalCustomerIdentifier;
}

$order_array =array(
'Order'=>array(
'CompanyIdentifier'=> $billing_address->getVatId(),
'OrderIdentifier'=>$_order->getIncrementId(),
'OrderType'=>1,
'Status'=>$_order->getStatus(),
'OrderDate'=>Mage::getModel('core/date')->date('Ymd', strtotime($_order->getCreatedAt())),
'OrderTotalAmount'=>$_order->getGrandTotal(),
'VatTotalAmount'=>$_order->getTaxAmount(),
'OrderTotalVatExcludedAmount'=>$_order->getSubtotal(),
'OrderInformation' => array(
'DeliveryDate' => Mage::getModel('core/date')->date('Ymd', strtotime($deliveryDate)),
'OrderFreeText' => '',
'OrderLanguage'=>$orderLanguage,
'CustomerOrderNo'=>$_order->getIncrementId(),
'OurReference'=>$_order->getIncrementId(),
'YourReference'=>'',
'DeliveryMethodText'=>$_order->getShippingMethod(),
'DeliveryTermsText'=>'Sopimuksen mukaan',
'SellerContactName'=>'Verkkokauppa',
'SellerContactNo'=>'5000001',
'OrderAccount'=>'',
),
'PaymentTerms' => array(
'PaymentTermsText' => $paymentTermsText,
'PaymentTermsNetDates' =>$paymentTermsNetDates,
'CashDiscountDates'=>0,
'CashDiscountPercent'=>0,
),
'InvoiceCustomer' => array(
'ExternalCustomerIdentifier' => $externalCustomerIdentifier,
'CustomerName1'=>$billing_address->getFirstname().' '.$billing_address->getMiddlename().' '.$billing_address->getLastname(),
'CustomerName2'=>'',
'CustomerAddress'=>$billing_address->getStreet(),
'CustomerPostCode'=>$billing_address->getPostcode(),
'CustomerTownName'=>$billing_address->getCity(),
'CustomerCountryCode'=>$billing_address->getCountryId(),
'CustomerCountryName'=>'',
'CustomerPhone2'=>$billing_address->getTelephone(),
'CustomerFax'=>$billing_address->getFax(),
'CustomerEmail'=>$billing_address->getEmail(),
'CustomerTaxCode'=>$billing_address->getVatId(),
'CustomerIdentifier'=>$customerIdentifier,
'CustomerFinvoiceAddress'=>'',
'CustomerFinvoiceIntermediator'=>''
),
'OrderRows' =>$orderRowsArray
)
);
// initializing or creating array
$student_info = $order_array;
// function call to convert array to xml
Mage::helper('hofler')->array_to_xml($student_info,$xml_student_info);
}
}

// $xmlName = $filesDir .date('Y-m-d').'.xml';
// echo $dom->save($xmlName);
$content = $xml_student_info->asXML();
$content = str_replace(array('<OrderDate>','<DeliveryDate>'),array('<OrderDate Format="YYYYMMDD">','<DeliveryDate Format="YYYYMMDD">'), $content);
// $content = $dom->saveXML();
echo $content."\r\n";

}

 

 

helper文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public function create_item($dom, $item, $data, $attribute=array()) {
if (is_array($data)) {
foreach ($data as $key => $val) {
$$key = $dom->createElement($key);
$item->appendchild($$key);

$text = $dom->createTextNode($val);
$$key->appendchild($text);

if (isset($attribute[$key])) {
foreach ($attribute[$key] as $akey => $row) {
$$akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);

$aval = $dom->createTextNode($row);
$$akey->appendChild($aval);
}
} // end if
}
} // end if
} // end function

// function defination to convert array to xml
function array_to_xml($student_info, &$xml_student_info) {
foreach($student_info as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_student_info->addChild("$key");
$this->array_to_xml($value, $subnode);
}
else{
$subnode = $xml_student_info->addChild("OrderRow");
$this->array_to_xml($value, $subnode);
}
}
else {
$xml_student_info->addChild("$key",htmlspecialchars("$value"));
}
}
}

 

 

注意:用DomDocument的话需要安装php扩展。而SimpleXMLElement不需要安装扩展

坚持原创技术分享,您的支持将鼓励我继续创作!