您好我想在我的帐户模板的WooCommerce模板“orders.php”中进行自定义订单查询。
您编辑的模板“my-theme / woocommerce / templates / myaccount / orders.php”
查询如下。但我不使用Woocommerce 3.0.x
$customer_orders = get_posts( array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => 'publish',
'date_query' => array(
array(
'after' => date('Y-m-d', strtotime($from)),
'before' => date('Y-m-d', strtotime($to. ' + 1 days'))
),
),
) );
可能有什么问题?
由于
答案 0 :(得分:2)
首先你应该Overriding WooCommerce Templates via your Theme,但不能直接插入
然后,此查询中的主要问题来自关于WooCommerce订单的post_status,这是非常具体的。
## DEFINING VARIABLES, JUST FOR TESTING ##
$order_count = -1;
$from = '2016/04/08';
$to = '2017/02/02';
因此,您现在应该使用经过测试的代码:
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
# HERE below set your desired Order statusses
'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed' ),
'date_query' => array( array(
'after' => date( 'Y-m-d', strtotime( $from ) ),
'before' => date( 'Y-m-d', strtotime( $to . ' + 1 days' ) ),
'inclusive' => true, // If you want a before date to be inclusive,
) ),
) );
或者您也可以使用专用的WooCommerce订单功能 wc_get_orders()
,这将为您提供所有 WC_Order
对象,而不是WP_Post
对象,这样:
$customer_orders = wc_get_orders( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
## NOT NEEDED ## 'post_type' => 'shop_order',
'post_status' => array( 'wc-pending', 'wc-processing', 'wc-on-hold', 'wc-completed' ),
'date_query' => array( array(
'after' => date( 'Y-m-d', strtotime( $from ) ),
'before' => date( 'Y-m-d', strtotime( $to . ' + 1 days' ) ),
'inclusive' => true, // If you want a before date to be inclusive,
) ),
) );
然后,您就可以直接在每个$ order对象上使用所有 WC_Order
方法] 2 ...