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
|
2 Answers
Reset to default 1You 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
版权声明:本文标题:WooCommerce Shop Orders by Date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302877a1931690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
date_query
parameter, which is very similar tometa_query
. See the WP_Query docs on Dates. – Howdy_McGee ♦ Commented Mar 16, 2022 at 4:40