How To Export Woocommerce Orders to XML Using PHP

This tutorial will show you how to export all your woocommerce shop orders in xml file. We will use woocommerce default functions to get all the data we need to generate the xml then when we will access a specific link the xml will be auto generated again and again. If we want to automate the process even more we can create a cron job and set the time period when our script should be run, in this way we will have all the time an up to date xml with all orders from  our shop. In the below function we will use a specific period for the orders because we don't need all orders to be in that xml, we will get for instance orders from last week or 2 weeks, anyway you can pass any data you need.

First of all you will need to create a new file in your root wordpress theme and we will call it XMLGenerator.php. Because we created a new file in wordpress, we don't have access to wordpress's functions so we will have to load the wordpress core in our file using the following code:

define('WP_USE_THEMES', true);
  /** Loads the WordPress Environment and Template */

With the above code your file should start, now that we have the environment set, we can proceed to the next step. We need to get all orders in a variable and from here we will start using woocommerce functions to grab all the data we need to insert in our xml. To get all orders from shop for a specific date range we will use the following function:

function get_orders_by_custom_date(){
    $now = strtotime('now');
  $today =  date("m/d/Y H:i:s",$now);
  $before_today = date('m/d/Y H:i:s', $now - 60 * 60 * 168);  
  $order_statuses = wc_get_order_statuses();
  $query_args = array(
        'post_type'      => array('shop_order'),
        'post_status'    => array_keys( $order_statuses ),
        'posts_per_page' => -1,
        'date_query' => array(
            'after'     => $before_today,
            'before'    => $today,
    $customer_orders = get_posts( $query_args );
  return $customer_orders;

Using the above function we will get all orders for custom post type orders. You have there all parameters and variables so you can change them as you wish. This function can be added to your functions.php file and we can just call it from our file.

Now, go back to your new created file and get all orders using the above function and save them in a variable:

$orders_ = get_orders_by_custom_date();

If we already have the wordpress envrionment in this file and all orders, from now is very easy to get all the data we need. In the below code will show you how to get some basic orders data and how to create the xml file and generate it.


$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';  
  $xmlString .= '<data>';
    foreach($orders_ as $k => $v):?>
        $order = wc_get_order( $v->ID );
        $order_id = $order->get_id();
        $order_total = $order->get_total();
        $order_key = $order->get_order_key();
        $order_date = date("Y-m-d h:m:s", strtotime($order->get_date_created()));
        $order_completed = $order->get_date_completed();
        $order_status = $order->get_status();
        $order_shipping = $order->get_shipping_methods();
        $order_coupons = $order->get_used_coupons();
        $customer_id = get_post_meta($order_id, '_customer_user', true);
        $order_shipping = $order->get_total_shipping();
        $order_payment_method = $order->get_payment_method();
        $billing_first_name = $order->get_billing_first_name();
        $billing_last_name = $order->get_billing_last_name();
        $billing_phone = get_post_meta($order_id, '_billing_phone', true);
        $billing_company = $order->get_billing_company();
        $billing_address = $order->get_billing_address_1();
        $billing_city = $order->get_billing_city();
        $billing_postcode = $order->get_billing_postcode();
        $billing_country = $order->get_billing_country();
        $billing_state = $order->get_billing_state();
        $billing_email = get_post_meta($order_id, '_billing_email', true);

        $xmlString .= '<OrderTotal>'.$order_total.'</OrderTotal>';
        $xmlString .= '<OrderKey>'.$order_key.'</OrderKey>';
        $xmlString .= '<Title>Order - '.$order_date.'</Title>';
        $xmlString .= '<OrderDate>'.$order_date.'</OrderDate>';
        $xmlString .= '<CompletedDate>'.$order_completed.'</CompletedDate>';
        $xmlString .= '<OrderStatus>'.$order_status.'</OrderStatus>';
        $xmlString .= '<OrderCurrency>USD</OrderCurrency>';
        $xmlString .= '<PaymentMethodTitle>'.$order_payment_method.'</PaymentMethodTitle>';
        $xmlString .= '<BillingFirstName>'.$billing_first_name.'</BillingFirstName>';
        $xmlString .= '<BillingLastName>'.$billing_last_name.'</BillingLastName>';
        $xmlString .= '<PhoneNumber>'.$billing_phone.'</PhoneNumber>';
        $xmlString .= '<EmailAddress>'.$billing_email.'</EmailAddress>';
        $xmlString .= '<BillingCompany>'.$billing_company.'</BillingCompany>';
        $xmlString .= '<BillingAddress1>'.$billing_address.'</BillingAddress1>';
        $xmlString .= '<BillingCity>'.$billing_city.'</BillingCity>';
        $xmlString .= '<BillingPostcode>'.$billing_postcode.'</BillingPostcode>';
        $xmlString .= '<BillingCountry>US</BillingCountry>';
        $xmlString .= '<BillingState>'.$billing_state.'</BillingState>';
        $xmlString .= '<BillingEmail>'.$billing_email.'</BillingEmail>';
        $xmlString .= '<BillingPhone>'.$billing_phone.'</BillingPhone>';
    <?php endforeach;
  $xmlString .= '</data>';

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;

//Save XML as a file

That's it, now run this file from your browser, I mean put the link to your file in browser and hit enter. the link should look like this:

After you access the link you should look in your wordpress root to get the file. As I said earlier it is not necessarily to access the file using browser you can create a cron job and it can access the file for you at a specific range time so you will have your xml updated all the time. 

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x