Reputation:
I want to find the time from order processing to order marked as complete for every order. How can I get that? I have read this and it only gives time for last modified.
Upvotes: 0
Views: 194
Reputation: 4243
This will get all the orders and return the number of seconds between the order being placed and if it is currently at a completed status. You haven't specified a format for the time difference so I have just returned it as a string with the corresponding order id.
$query = new WC_Order_Query( array(
'limit' => -1,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
foreach( $query->get_orders() as $order_id ) {
$order = wc_get_order($order_id);
if( $order->get_status() === 'completed') {
$order_data = $order->get_data();
$timestamp = $order_data['date_modified']->getTimestamp() - $order_data['date_created']->getTimestamp();
$d1 = new DateTime();
$d2 = new DateTime();
$d2->add(new DateInterval('PT'.$timestamp.'S'));
$order_interval = $d2->diff($d1);
echo 'Order ID : ' . $order->get_id() . ' ' . $order_interval->format('%a days, %h hours, %i minutes and %s seconds');
}
}
Upvotes: 1
Reputation: 65274
have you tried this?
$order_id = 1945;
$order = new WC_Order($order_id);
$order_data = $order->get_data();
$date1 = $order_data['date_created']->date( 'Y-m-d G:i:s' );
$date2 = $order_data['date_modified']->date( 'Y-m-d G:i:s' );
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("%d years, %d months, %d days\n", $years, $months, $days);
You can improved this by adding a check for the status of the order.
Upvotes: 0