2017-10-24 19:08:34 +00:00
|
|
|
(function( $, _, undefined ) {
|
|
|
|
|
|
|
|
if ( 'undefined' === typeof woocommerce_network_orders ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var orders = [],
|
2017-10-25 03:45:34 +00:00
|
|
|
promises = [], // Track completion (pass or fail) of ajax requests
|
|
|
|
deferred = [], // Tracks the ajax deferreds
|
2017-10-24 19:08:34 +00:00
|
|
|
$tbody = $( document.getElementById( 'network-orders-tbody' ) ),
|
|
|
|
template = _.template( $( document.getElementById( 'network-orders-row-template') ).text() );
|
|
|
|
|
|
|
|
// No sites, so bail
|
|
|
|
if ( ! woocommerce_network_orders.sites.length ) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
$.each( woocommerce_network_orders.sites, function( index, value ) {
|
2017-10-25 03:45:34 +00:00
|
|
|
promises[ index ] = $.Deferred();
|
2017-10-24 19:08:34 +00:00
|
|
|
deferred.push( $.ajax( {
|
|
|
|
url : woocommerce_network_orders.order_endpoint,
|
|
|
|
data: {
|
|
|
|
_wpnonce: woocommerce_network_orders.nonce,
|
|
|
|
network_orders: true,
|
|
|
|
blog_id: value
|
|
|
|
},
|
|
|
|
type: 'GET'
|
|
|
|
} ).success(function( response ) {
|
|
|
|
var orderindex;
|
|
|
|
|
|
|
|
for ( orderindex in response ) {
|
|
|
|
orders.push( response[ orderindex ] );
|
|
|
|
}
|
2017-10-25 03:45:34 +00:00
|
|
|
|
|
|
|
promises[ index ].resolve();
|
|
|
|
}).fail(function (){
|
|
|
|
promises[ index ].resolve();
|
2017-10-24 19:08:34 +00:00
|
|
|
}) );
|
|
|
|
} );
|
|
|
|
|
2017-10-25 03:45:34 +00:00
|
|
|
if ( promises.length > 0 ) {
|
|
|
|
$.when.apply( $, promises ).done( function() {
|
2017-10-24 19:08:34 +00:00
|
|
|
var orderindex,
|
|
|
|
currentOrder;
|
|
|
|
|
2017-11-06 17:01:02 +00:00
|
|
|
// Sort orders, newest first
|
|
|
|
orders.sort(function( a, b ) {
|
|
|
|
var adate, bdate;
|
|
|
|
|
|
|
|
adate = Date.parse( a.date_created_gmt );
|
|
|
|
bdate = Date.parse( b.date_created_gmt );
|
|
|
|
|
|
|
|
if ( adate === bdate ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( adate < bdate ) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-10-24 19:08:34 +00:00
|
|
|
for ( orderindex in orders ) {
|
|
|
|
currentOrder = orders[ orderindex ];
|
|
|
|
|
|
|
|
$tbody.append( template( currentOrder ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
})( jQuery, _ );
|