Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woogist/6676058 to your computer and use it in GitHub Desktop.
Save woogist/6676058 to your computer and use it in GitHub Desktop.
WooCommerce Customer/Order XML Export Suite: Custom Plugin for changing the XML output
<?php
/**
* Plugin Name: WooCommerce Sample XML
* Plugin URI: http://www.skyverge.com/contact/
* Description: Customizes the WooCommerce Customer/Order XML Export Suite specifically for Sample
* Author: SkyVerge
* Author URI: http://www.skyverge.com
* Version: 1.0
*
* Copyright: (c) 2013 SkyVerge, Inc. (info@skyverge.com)
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package WC-Sample-XML
* @author SkyVerge
* @category Custom
* @copyright Copyright (c) 2013, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*/
/**
* 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(
'OrderList' => array(
'@attributes' => array(
'StoreName' => get_home_url(),
),
'Order' => $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(
'@attributes' => array( 'currency' => 'USD', 'type' => 'Local', 'id' => $order->id ),
'Time' => date( 'Y-m-d H:i:s', strtotime( $order->order_date ) ),
'NumericTime' => strtotime( $order->order_date ),
'Origin' => 'Local',
'AddressInfo' => array(
0 => array(
'@attributes' => array( 'type' => 'ship' ),
'Name' => array(
'First' => $order->shipping_first_name,
'Last' => $order->shipping_last_name,
'Full' => $order->shipping_first_name . ' ' . $order->shipping_last_name,
),
'Address1' => $order->shipping_address_1,
'Address2' => $order->shipping_address_2,
'City' => $order->shipping_city,
'State' => $order->shipping_state,
'Country' => $order->shipping_country,
'Zip' => $order->shipping_postcode,
'Phone' => $order->billing_phone,
'Email' => $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_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;
}
/* The above code will produce an XML file that looks like:
<?xml version="1.0" encoding="UTF-8"?>
<OrderList StoreName="http://woo.com">
<Order currency="USD" type="Local" id="1884">
<Time>2013-09-14 15:38:26</Time>
<NumericTime>1379173106</NumericTime>
<Origin>Local</Origin>
<AddressInfo type="ship">
<Name>
<First>Steve</First>
<Last>Jobs</Last>
<Full>Steve Jobs</Full>
</Name>
<Address1>1 Infinite Loop</Address1>
<Address2></Address2>
<City>Cupertino</City>
<State>CA</State>
<Country>US</Country>
<Zip>90143</Zip>
<Phone>414-398-1412</Phone>
<Email>steve@apple.com</Email>
</AddressInfo>
<AddressInfo type="bill">
<Name>
<First>Steve</First>
<Last>Jobs</Last>
<Full>Steve Jobs</Full>
</Name>
<Address1>1 Infinite Loop</Address1>
<Address2></Address2>
<City>Cupertino</City>
<State>CA</State>
<Country>US</Country>
<Zip>90143</Zip>
<Phone>414-398-1412</Phone>
<Email>steve@apple.com</Email>
</AddressInfo>
<Shipping>Free Shipping</Shipping>
<Comments></Comments>
<Items>
<Id>W0027</Id>
<Quantity>1</Quantity>
<Unit-Price>79.99</Unit-Price>
<Description>Battlefield 5</Description>
<Cost>49.99</Cost>
<Url>http://www.amazon.com/battlefield5</Url>
<Taxable>YES</Taxable>
</Items>
<Total>
<Line type="Coupon" name="Discount">0.00</Line>
<Line type="Shipping" name="Shipping">0.00</Line>
<Line type="Tax" name="Tax">4.00</Line>
<Line type="Total" name="Total">83.99</Line>
</Total>
<PONumber></PONumber>
</Order>
</OrderList>
*/
@peterwa
Copy link

peterwa commented Jan 8, 2014

Help me!
how do i get artical attributes in xml file?

@solaceten
Copy link

Thank you for this very helpful little plugin!

It would also be nice to find a way of adding in the total line items in the order. Quantity per item is all good, but I need to show number of items total as well, e.g. 6 overall items in the order, 2 apples, 2 oranges, 1 pear and 1 banana) - is that possible ?

example:

<ItemsTotal>
<OrderTotal>6</OrderTotal>
</ItemsTotal>
<Items>
<Id>W0027</Id>
<Quantity>2</Quantity>
<Unit-Price>.99</Unit-Price>
<Description>Apple</Description>
<Cost>49.99</Cost>
</Items> 

@gagimilicevic
Copy link

How can I modify this code so I can group for example only customer details in separate tag?

Billing details - customer data
ordered product items

Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment