2
    $orders=wc_get_orders( array( 'numberposts' => -1,'orderby'=>'date','order'=>'ASC'));
    foreach($orders as $order) {

        $order_id = $order->id;
        $detail = wc_get_order( $order_id );
        $order_status = $detail->get_status();
        $od = $detail->get_date_created(); 
    }

i am trying to get "order-created-date" but its giving me null value. 

is there anything wrong in my code? can i get some help please !

sam707
  • 75
  • 1
  • 6

1 Answers1

3

This should be pretty easy. Try this.

$orders = wc_get_orders( array( 'numberposts' => -1,'orderby'=>'date','order'=>'ASC'));

foreach($orders as $order) {
    $order_status = $order->get_status();
    $od = $order->get_date_created();
}

No need to get ID and then again call for orders object.


I would recommend not to use 'numberposts' => -1 as its a performance nightmare. You can learn more about that here : https://10up.github.io/Engineering-Best-Practices/php/#performance

bhanu
  • 1,730
  • 3
  • 12
  • 30
  • thanks for the response.. and one more thing .. what "Type" do i need to choose for saving this "date value" in mysql table ? @bhanu – sam707 Jul 08 '21 at 13:01
  • 1
    This is a WC_DateTime object you can learn more about it [here](https://woocommerce.github.io/code-reference/classes/WC-DateTime.html). Is totally depends on your database management choices. This `$order->get_date_created()->date('Y-m-d h:i:s');` should give you date like this `2021-06-22 03:34:50` . You might also want to store the timezone in case that is relvent for your business logic. Learn more here : https://stackoverflow.com/a/48979441/8298248 – bhanu Jul 08 '21 at 14:03
  • Also if this answer was correct. Do mark this as the correct answer. This is how you can do it : https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235 @sam707 – bhanu Jul 08 '21 at 14:04