How to get the order date in woocommerce?

Member

by lizzie , in category: PHP CMS , 7 months ago

How to get the order date in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by darrion.kuhn , 7 months ago

@lizzie 

To get the order date in WooCommerce, you can use the following code snippet:

1
2
3
4
5
6
$order_id = 100; // replace this with the actual order ID

$order = wc_get_order( $order_id );
$order_date = $order->get_date_created();

echo $order_date->date_i18n( get_option( 'date_format' ) );


You can replace the $order_id variable with the actual order ID you want to retrieve the date for. This code snippet will fetch the order object using wc_get_order() function, and then retrieve the order creation date using get_date_created() method. Finally, the date is formatted using date_i18n() function with the WordPress's date format settings.


You can place this code in your theme's functions.php file or a custom plugin to display the order date on the order details page or anywhere else you need it.