Created
June 21, 2018 11:34
-
-
Save kierzniak/5639b94ab73d6dbd75a9e28ff0033b23 to your computer and use it in GitHub Desktop.
Export WooCommerce orders to XML file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Export WooCommerce orders to xml file | |
* | |
* @return void|string Return nothing or xml file | |
*/ | |
function motivast_export_xml_with_woocommerce_orders() { | |
/** | |
* Check for credentials or IP address to allow download | |
* xml for particular users only. | |
*/ | |
$allowed_ip = '127.0.0.1'; | |
if ( get_current_ip() !== $allowed_ip ) { | |
return; | |
} | |
/** | |
* If there is not export parameter in url do nothing | |
*/ | |
$export = filter_input( INPUT_GET, 'export_xml', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); | |
if( true !== $export ) { | |
return; | |
} | |
/** | |
* Get last 10 orders. | |
*/ | |
$orders_to_xml = array(); | |
$query = new WC_Order_Query(); | |
$orders = $query->get_orders(); | |
/** | |
* Iterate over orders to prepare xml ready array | |
*/ | |
foreach ( $orders as $order ) { | |
$orders_to_xml['status'] = $order->get_status(); | |
$orders_to_xml['total'] = $order->get_total(); | |
$orders_to_xml['items'] = array(); | |
$items = $order->get_items(); | |
foreach( $items as $key => $item ) { | |
$orders_to_xml['items'][$key]['product_id'] = $item->get_id(); | |
$orders_to_xml['items'][$key]['name'] = $item->get_name(); | |
} | |
} | |
/** | |
* Create XML | |
*/ | |
$xml = new SimpleXMLElement('<?xml version="1.0"?><root></root>'); | |
array_to_xml( $orders_to_xml, $xml ); | |
echo $xml->asXML(); | |
/** | |
* Terminate to not print anything else | |
*/ | |
exit; | |
} | |
add_action('init', 'motivast_export_xml_with_woocommerce_orders'); | |
/** | |
* Generate xml document from array | |
* | |
* @param array $data Array to convert | |
* @param SimpleXMLElement $xml XML document to work on | |
*/ | |
function array_to_xml( $data, &$xml ) { | |
foreach( $data as $key => $value ) { | |
if( is_numeric( $key ) ){ | |
$key = 'item'; | |
} | |
if( is_array($value) ) { | |
$subnode = $xml->addChild( $key ); | |
array_to_xml( $value, $subnode ); | |
} else { | |
$xml->addChild( $key, htmlspecialchars( $value ) ); | |
} | |
} | |
} | |
/** | |
* Get current IP | |
* | |
* @return string Current IP address | |
*/ | |
function get_current_ip() { | |
$http_client_ip = filter_input( INPUT_SERVER, 'HTTP_CLIENT_IP', FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE ); | |
$http_forwarded_for = filter_input( INPUT_SERVER, 'HTTP_X_FORWARDED_FOR', FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE ); | |
$remote_addr = filter_input( INPUT_SERVER, 'REMOTE_ADDR', FILTER_VALIDATE_IP, FILTER_NULL_ON_FAILURE ); | |
return $http_client_ip ? $http_client_ip : ($http_forwarded_for ? $http_forwarded_for : $remote_addr ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment