Home > database >  Get all the order ids that are completed in a day by wc_get_orders in woocomerce
Get all the order ids that are completed in a day by wc_get_orders in woocomerce

Time:04-21

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); 

CodePudding user response:

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'

CodePudding user response:

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

  • Related