Is there a way to get the exact order date and time to show up in woocommerce emails? So far I am using this to get the order date:
<?php printf( '<time datetime="%s">%s</time>', $order->get_date_created()->format( 'c' ), wc_format_datetime( $order->get_date_created() ) ); ?>
I get the correct date however no timestamp when the order was placed. How do I add the exact time stamp?
Something like this:
Order NO. XXXX (placed on February 25, 2018 10:06PM EST)
There is 4 different date cases when an order is placed (where $order
is an instance of the WC_order
Object) depending on order status, on the payment method used and on Woocommerce regarding behavior:
$order->get_date_created()
$order->get_date_modified()
$order->get_date_paid()
$order->get_date_completed()
All this 4 orders different dates are WC_DateTime
objects (instances) where you can use available WC_DateTime
methods.
To get the correct format to have something like:
Order NO. XXXX (placed on February 25, 2018 10:06PM EST)
… you will use the for example the following:
$date_modified = $order->get_date_modified();
echo sprintf( '<p>Order NO. %s (placed on <time>%s</time>)</p>',
$order->get_order_number( ),
$date_modified->date("F j, Y, g:i:s A T")
);
If you want to use
get_date_paid()
orget_date_completed()
methods you should need to do it carefully, testing that theWC_DateTime
object exist before trying to display it…
$date_paid = $order->get_date_paid();
if( ! empty( $date_paid) ){
echo sprintf( '<p>Order NO. %s (placed on <time>%s</time>)</p>',
$order->get_order_number( ),
$date_paid->date("F j, Y, g:i:s A T")
);
}
As you don't specify where exactly specifically you want this to be displayed for customer processing order email notification, I will give you an example of hooked function that you can use:
add_action( 'woocommerce_email_order_details', 'custom_processing_order_notification', 1, 4 );
function custom_processing_order_notification( $order, $sent_to_admin, $plain_text, $email ) {
// Only for processing email notifications to customer
if( ! 'customer_processing_order' == $email->id ) return;
$date_modified = $order->get_date_modified();
$date_paid = $order->get_date_paid();
$date = empty( $date_paid ) ? $date_modified : $date_paid;
echo sprintf( '<p>Order NO. %s (placed on <time>%s</time>)</p>',
$order->get_order_number( ),
$date->date("F j, Y, g:i:s A T")
);
}
Code goes in function.php file of your active child theme (or theme).
Tested and works.