I'm customizing a WordPress plugin to export a JSON file for 3rd party use.
I need to add an array to "order_item" where it contain only one item. For the multiple order items, the array (square brackets) is automatically added.
I try different methods to include array to $child->addChild( 'order_item' ), for example (array)$child->addChild( 'order_item' ) but it should be wrong method.
a function to generate the json output is as follow:
function woo_ce_export_dataset_override_order( $output = null ) {
global $export;
if( !empty( $export->fields ) ) {
$child = $output->addChild( 'transaction_date', date("Ymd", time()) );
foreach( $orders as $order ) {
$child = $output->addChild( apply_filters( 'woo_ce_export_xml_order_node', 'neworders' ) );
$args = $export->args;
$order = woo_ce_get_order_data( $order, 'order', $args, array_keys( $export->fields ) );
foreach( array_keys( $export->fields ) as $key => $field ) {
if( isset( $order->$field ) && isset( $export->columns[$key] ) ) {
if( !is_array( $field ) ) {
$child->addChild( apply_filters( 'woo_ce_export_xml_order_label', sanitize_key( $export->columns[$key] ), $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $order->$field ) ) );
}
}
}
if( !empty( $order->order_items ) ) {
foreach( $order->order_items as $order_item ) {
$order_item_child = $child->addChild( 'order_item' );
foreach( array_keys( $export->fields ) as $key => $field ) {
if( isset( $order_item->$field ) && isset( $export->columns[$key] ) ) {
if( !is_array( $field ) ) {
$order_item_child->addChild( apply_filters( 'woo_ce_export_xml_order_label', sanitize_key( $export->columns[$key] ), $export->columns[$key] ), esc_html( woo_ce_sanitize_xml_string( $order_item->$field ) ) );
}
}
}
}
}
}
// Allow Plugin/Theme authors to add support for sorting Orders
$output = apply_filters( 'woo_ce_orders_output', $output, $orders );
}
return $output;
}
This is what i get from the output:
{
"transaction_date": "20190607",
"neworders": [
{
"postid": "12081",
"order_item": [
{
"ugs": "SAM1222",
"qty": "3"
},
{
"ugs": "NOK8777",
"qty": "3"
}
]
},
{
"postid": "12082",
"order_item": {
"ugs": "SON7411",
"qty": "1"
}
}
]
}
What i expected to include the array in order_item for postid: 12082
{
"transaction_date": "20190607",
"neworders": [
{
"postid": "12081",
"order_item": [
{
"ugs": "SAM1222",
"qty": "3"
},
{
"ugs": "NOK8777",
"qty": "3"
}
]
},
{
"postid": "12082",
"order_item": [
{
"ugs": "SON7411",
"qty": "1"
}
]
}
]
}