按日期和状态woocommerce获取订单

问题描述 投票:1回答:2

我需要获得woocommerce中传递开始日期,结束日期和状态的订单列表。

我尝试使用Mike Jolley所描述的某些技术,并与this混合使用。但是我还没有成功。这将返回所有订单。我正在使用woocommerce版本2.2.10。

感谢您的帮助。

我的代码:

public function get_orders(){
        global $json_api;
        $initial_date = $json_api->query->para1;
        $final_date = $json_api->query->para2;
        $order_id = $json_api->query->para3;
        $status_order = $json_api->query->para4;

        define('GET_ORDERS_FILTER_DATE_FROM', $initial_date );
        define('GET_ORDERS_FILTER_DATE_TO', $final_date );
        add_filter('posts_where', array( __CLASS__, 'get_orders_where_dates_between') );
        $orders = get_posts( array(
            'post_type'   => 'shop_order',
            'orderby' => 'post_date',
            'order'     => 'DESC',
            'post_status' => array_keys( $status_order )
        ) );
        remove_filter('posts_where', 'order_page_get_orders_where_dates_between');

        return $orders;
}

function get_orders_where_dates_between( $where ){
        global $wpdb;
        if( ! defined('GET_ORDERS_FILTER_DATE_FROM') || ! defined('PARCELWARE_GET_ORDERS_FILTER_DATE_TO') )
            return $where;

        $where .= $wpdb->prepare(" AND post_date >= '%s' ", GET_ORDERS_FILTER_DATE_FROM);
        $where .= $wpdb->prepare(" AND post_date <= '%s' ", GET_ORDERS_FILTER_DATE_TO);

        return $where;
}
php wordpress wordpress-plugin woocommerce
2个回答
7
投票

如何使用这样的自定义wpdb查询?

global $wpdb;

$date_from = '2015-11-20';
$date_to = '2015-12-20';
$post_status = implode("','", array('wc-processing', 'wc-completed') );

$result = $wpdb->get_results( "SELECT * FROM $wpdb->posts 
            WHERE post_type = 'shop_order'
            AND post_status IN ('{$post_status}')
            AND post_date BETWEEN '{$date_from}  00:00:00' AND '{$date_to} 23:59:59'
        ");

echo "<pre>";
print_r($result);

0
投票

您可以使用wc_get_orders

$initial_date = yyyy-mm-dd;
$final_date = yyyy-mm-dd;
wc_get_orders(array(
    'limit'=>-1,
    'type'=> 'shop_order',
    'status'=> array( 'wc-completed','wc-refunded' ),
    'date_created'=> $initial_date .'...'. $final_date 
    )
);
© www.soinside.com 2019 - 2024. All rights reserved.