skip to Main Content

I am not getting the order ids that are completed on a date when using wc_get_orders, if there are 5 orders completed in the day 2022-04-19, then I need to get all the 5 order ids by using the date 2022-04-19. I have tried the below code but it’s not working

$args = array(
    'status' => "Completed",
    'return' => 'ids',
    'date_completed' => '2022-04-19',
);
$orders = wc_get_orders( $args );

print_r($orders); 

2

Answers


  1. Chosen as BEST ANSWER

    I have tried a lot with this and I can't get a correct answer with wc_get_orders or WC_Order_Query. I think it may be an issue with the date comparison. I have sorted the issue by taking the order id by SQL query

    global $wpdb;
    $resultnew = $wpdb->get_results( "SELECT post_id FROM `wp_postmeta` WHERE `meta_key` = '_completed_date' AND CAST(`meta_value` AS date)='{$date}'and post_id in (SELECT ID FROM `wp_posts` WHERE post_type = 'shop_order' and post_status='wc-completed');");
    

    $date will be like 2022-04-19


  2. You can use WC_Order_Query instead of using wc_get_orders

    <?php
    $args = array(
     'limit' => 9999,
     'return' => 'ids',
     'date_completed' => '2022-04-19',
     'status' => 'wc-completed'
    );
    $query = new WC_Order_Query( $args );
    $orders = $query->get_orders();
    

    And according to WooCommerce Code Reference (https://woocommerce.github.io/code-reference/files/woocommerce-includes-wc-order-functions.html#source-view.93) order status should be ‘wc-completed’

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search