18

I can see inside class-wc-admin-cpt-shop_order.php there are some functions that are pulling together the order information for display in WooCommerce. However, I don't see anywhere where the date can be used ...

Because WooCommerce uses wp_posts to store the data, can I assume that the post_date field is the correct one to use?

Also, anyone know whether there is a function in WooCommerce to get this, or whether there is a way of getting the date to come out in class-wc-admin-cpt-shop_order.php.

Ke.
  • 2,484
  • 8
  • 40
  • 78
  • Hi d74p, your suggested change to start the sentence with "because" instead of "since" is grammatically incorrect. – Ke. Jul 31 '15 at 18:29

3 Answers3

29
// Get $order object from order ID 
$order = wc_get_order( $order_id );

// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();

Source: https://businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/


Additionally

$order->get_date_created();

Is the "order date", which you can change within WooCommerce ("Edit Order")

enter image description here

optimiertes
  • 4,002
  • 1
  • 22
  • 15
20

You can use the WC_Order object, if you have the order ID:

$order = new WC_Order($order_id);
$order_date = $order->order_date;
rnevius
  • 26,578
  • 10
  • 58
  • 86
  • 4
    Alternatively, you can use `wc_get_order( $order_id );` – helgatheviking Jul 31 '15 at 14:29
  • @helgatheviking , what's the advantage of using `wc_get_order()` in this case? As far as I know, `wc_get_order()` would just end up calling `get_post()`, since the supplied argument is numeric. It's possible I'm missing something. – rnevius May 26 '16 at 19:30
  • 2
    `new WC_Order($order_id)` is also going to call [`get_post()`](https://github.com/woothemes/woocommerce/blob/53a1538d1b46cc5234c407880175e06f13b33a0e/includes/abstracts/abstract-wc-order.php#L117) if the argument is numeric. It's a little I say tomato you say tomahto.... ie, almost no difference. If you forced me I'd say the `wc_get_order()` will probably not change and if they changed something about the `WC_Order()` class the wrapper would still work. – helgatheviking May 26 '16 at 21:06
  • Is there an "order_completed_date" field? – Garconis Aug 29 '17 at 17:37
  • To answer my own question above, `get_date_completed(); ?>` is what I needed. – Garconis Aug 29 '17 at 17:45
  • Use woocommerce 3 and above, Order properties should not be accessed directly. – Remi Sep 23 '19 at 10:33
  • It cannot be accessed directly. – David May 15 '21 at 17:19
9

Order properties should not be accessed directly. Best way is $order->get_date_completed()

devugur
  • 1,339
  • 1
  • 19
  • 25