我完全失去了XML,我需要使用Woocommerce插件生成XML。將XML添加到PHP插件中
比如我想這行補充:
<?xml-stylesheet type="text/xsl" href="commande.xsl"?>
這個PHP腳本:
/**
* Adjust the root-level XML format
*
* @since 1.0
* @param array $orders_format existing order array format
* @param array $orders array \WC_Order
* @return array
*/
function wc_sample_xml_order_format($orders_format, $orders) {
$orders_format = array(
'COMMANDE' => array(
'@attributes' => array(
),
'ENTETE' => $orders,
),
);
return $orders_format;
}
add_filter('wc_customer_order_xml_export_suite_order_export_format', 'wc_sample_xml_order_format', 10, 2);
/**
* Adjust the individual order format
*
* @since 1.0
* @param array $order_format existing order array format
* @param object $order \WC_Order instance
* @return array
*/
function wc_sample_xml_order_list_format($order_format, $order) {
return array(
'NO_COMMANDE' => $order->id,
'NumericTime' => strtotime($order->order_date),
'Origin' => 'Local',
'AddressInfo' => array(
0 => array(
'@attributes' => array('type' => 'ship'),
'NOM_CLIENT' => $order->shipping_first_name . ' ' . $order->shipping_last_name,
'ADRESSE_1' => $order->shipping_address_1,
'ADRESSE_2' => $order->shipping_address_2,
'VILLE' => $order->shipping_city,
'PROVINCE' => $order->shipping_state,
'PAYS' => $order->shipping_country,
'CODE_POSTAL' => $order->shipping_postcode,
'TELEPHONE' => $order->billing_phone,
'COURRIEL' => $order->billing_email,
),
1 => array(
'@attributes' => array('type' => 'bill'),
'Name' => array(
'First' => $order->billing_first_name,
'Last' => $order->billing_last_name,
'Full' => $order->billing_first_name . ' ' . $order->billing_last_name,
),
'Address1' => $order->billing_address_1,
'Address2' => $order->billing_address_2,
'City' => $order->billing_city,
'State' => $order->billing_state,
'Country' => $order->billing_country,
'Zip' => $order->billing_postcode,
'Phone' => $order->billing_phone,
'Email' => $order->billing_email,
),
),
'Shipping' => $order->get_shipping_method(),
'Comments' => $order->customer_note,
'Items' => wc_sample_xml_get_line_items($order),
'Total' => array(
'Line' => array(
0 => array(
'@attributes' => array('type' => 'Coupon', 'name' => 'Discount'),
'@value' => $order->get_total_discount()
),
1 => array(
'@attributes' => array('type' => 'Shipping', 'name' => 'Shipping'),
'@value' => $order->get_total_shipping()
),
2 => array(
'@attributes' => array('type' => 'Tax', 'name' => 'Tax'),
'@value' => $order->get_total_tax()
),
3 => array(
'@attributes' => array('type' => 'Total', 'name' => 'Total'),
'@value' => $order->get_total()
)
),
),
'PONumber' => get_post_meta($order->id, 'PO Number', true), // add your own order meta here
);
}
add_filter('wc_customer_order_xml_export_suite_order_export_order_list_format', 'wc_sample_xml_order_list_format', 10, 2);
/**
* Adjust the individual line item format
*
* @since 1.0
* @param object $order \WC_Order instance
* @return array
*/
function wc_sample_xml_get_line_items($order) {
foreach($order->get_items() as $item_id => $item_data) {
$product = $order->get_product_from_item($item_data);
$items[] = array(
'Id' => $product->get_sku(),
'Quantity' => $item_data['qty'],
'Unit-Price' => $product->get_price(),
'Description' => $product->get_title(),
'Cost' => woocommerce_get_order_item_meta($item_id, '_wc_cog_cost', true),
'Url' => set_url_scheme(get_permalink($product->id), 'http'),
'Taxable' => ($product->is_taxable()) ? 'YES' : 'NO'
);
}
return $items;
}
這是我們將用來生成訂單,並通過送他們一個插件我們用來處理訂單和運輸的軟件。系統需要以非常特定的格式接收XML文件。
非常感謝您的幫助!
是什麼格式的特定格式,你只是想顯示(回聲)您的網頁上的XML? –
嗨Olavi!實際上,該插件會生成一個XML文件,但是該XML文件中缺少一些元素,例如<?xml-stylesheet type =「text/xsl」href =「commande.xsl」?>。我不太確定如何在生成文件時自動添加此行。 –