admin管理员组

文章数量:1122832

I need to get WooCommerce orders and filter it between dates but its not working.

Here's my code:

$start = '2022-03-00';
$end = '2022-12-20';

$args = array(
    'post_type' => 'shop_order',
    'posts_per_page' => -1,
    'after'     => $start, 
    'before'    => $end,
    'meta_query' => array(
        array(
            'key' => '_order_send_time',
            'value' => array($start, $end),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        ),
        array(
            'key' => '_fb_pickup_branch',
            'value' => "900",
            'compare' => 'like',
        )
    )
);
// query
$events_query = new WP_query();
$events_query->query($orderArgs);

I need to get WooCommerce orders and filter it between dates but its not working.

Here's my code:

$start = '2022-03-00';
$end = '2022-12-20';

$args = array(
    'post_type' => 'shop_order',
    'posts_per_page' => -1,
    'after'     => $start, 
    'before'    => $end,
    'meta_query' => array(
        array(
            'key' => '_order_send_time',
            'value' => array($start, $end),
            'compare' => 'BETWEEN',
            'type' => 'DATE'
        ),
        array(
            'key' => '_fb_pickup_branch',
            'value' => "900",
            'compare' => 'like',
        )
    )
);
// query
$events_query = new WP_query();
$events_query->query($orderArgs);
Share Improve this question edited Mar 16, 2022 at 4:41 Howdy_McGee 20.8k24 gold badges91 silver badges175 bronze badges asked Mar 15, 2022 at 15:24 Fras JafFras Jaf 112 bronze badges 1
  • You may be able to use the date_query parameter, which is very similar to meta_query. See the WP_Query docs on Dates. – Howdy_McGee Commented Mar 16, 2022 at 4:40
Add a comment  | 

2 Answers 2

Reset to default 1

You should use wc_get_orders and make use of their built in date_before and date_after arguments.

$orders = wc_get_orders(
    'post_type' => 'shop_order',
    'posts_per_page' => -1,
    'date_before' => $before,
    'date_after' => $after,
);
var_dump($orders);

WooCommerce orders between specific dates, you can use the date_created parameter in the query And Add also meta_query as per need for result using key pair _fb_pickup_branch

// Define start and end dates
$start_date = '2022-01-01';
$end_date = '2022-01-31';

// Create a query to get orders between dates
$args = array(
    'post_type'      => 'shop_order',
    'post_status'    => 'wc-completed', // Adjust the order status as needed
    'date_query'     => array(
        'after'     => $start_date,
        'before'    => $end_date,
        'inclusive' => true, // Include orders on the start and end dates
    ),
    'nopaging'       => true, // Retrieve all orders, remove if you want pagination
);

// Get orders using WP_Query
$orders_query = new WP_Query($args);

// Check if there are orders
if ($orders_query->have_posts()) {
    while ($orders_query->have_posts()) {
        $orders_query->the_post();

        // Get order details as needed
        $order_id = $orders_query->post->ID;
        $order_date = $orders_query->post->post_date;

        // Process the order data as needed
        echo "Order ID: $order_id, Date: $order_date\n";
    }
} else {
    echo 'No orders found.';
}

// Restore global post data
wp_reset_postdata();

本文标签: WooCommerce Shop Orders by Date