嗨,我想在"我的帐户"模板的WooCommerce模板"订单.php"中进行自定义订单查询。
您编辑的模板"我的主题/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'))
),
),
) );
可能出了什么问题?
谢谢
首先,您应该通过主题覆盖WooCommerce模板,但不要直接在插件中
覆盖然后,此查询中的主要问题来自有关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...