获取Woocommerce中的每日订单数量

时间:2018-02-28 11:10:19

标签: php wordpress woocommerce count orders

我希望每当订单发布时,每天都会获得每日订单数量。我写了下面的代码,以便在有订单时给出关于松弛的通知。由于显示$ result,它显示当前订单金额而不是当天的总订单数量。

function wp_slack_woocommerce_order_status_completed2( $events ) {
    $events['woocommerce_order_status_processing'] = array(

        // Action in WooCommerce to hook in to get the message.
        'action' => 'woocommerce_order_status_processing',

        // Description appears in integration setting.
        'description' => __( 'Whenever we receive an order', 'slack-woocommerce' ),

        // Message to deliver to channel. Returns false will prevent
        // notification delivery.
        'message' => function( $order_id ) {
            $order = wc_get_order( $order_id );

            $date = is_callable( array( $order, 'get_date_completed' ) )
                ? $order->get_date_completed()
                : $order->completed_date;
            $url  = add_query_arg(
                array(
                    'post'   => $order_id,
                    'action' => 'edit',
                ),
                admin_url( 'post.php' )
            );

            $user_id = is_callable( array( $order, 'get_user_id' ) )
                ? $order->get_user_id()
                : $order->user_id;

            if ( $user_id ) {
                $user_info = get_userdata( $user_id );
            }

            if ( ! empty( $user_info ) ) {
                if ( $user_info->first_name || $user_info->last_name ) {
                    $username = esc_html( ucfirst( $user_info->first_name ) . ' ' . ucfirst( $user_info->last_name ) );
                } else {
                    $username = esc_html( ucfirst( $user_info->display_name ) );
                }
            } else {
                $billing_first_name = is_callable( array( $order, 'get_billing_first_name' ) )
                    ? $order->get_billing_first_name()
                    : $order->billing_first_name;
                $billing_last_name = is_callable( array( $order, 'get_billing_last_name' ) )
                    ? $order->get_billing_last_name()
                    : $order->billing_last_name;

                if ( $billing_first_name || $billing_last_name ) {
                    $username = trim( $billing_first_name . ' ' . $billing_last_name );
                } else {
                    $username = __( 'Guest', 'slack-woocommerce' );
                }
            }
            global $wpdb;

            $date_from = '2018-02-27';
            $date_to = '2018-02-28';
            $post_status = implode("','", array('wc-processing', 'wc-completed') );

            $result = $wpdb->get_results( "SELECT count(*) as total 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'
                    ");

            // Remove HTML tags generated by WooCommerce.
            add_filter( 'woocommerce_get_formatted_order_total', 'wp_strip_all_tags', 10, 1 );
            $total = html_entity_decode( $order->get_formatted_order_total() );
            remove_filter( 'woocommerce_get_formatted_order_total', 'wp_strip_all_tags', 10 );
            //$data2=WC_API_Reports::get_sales_report(  );
            // Returns the message to be delivered to Slack.
            return apply_filters( 'slack_woocommerce_order_status_completed_message',
                sprintf(
                    __( 'Reveived new order with amount *%1$s*. Made by *%2$s* on *%3$s*. <%4$s|See detail> %s', 'slack-woocommerce' ),
                    $total,
                    $username,
                    $date,
                    $url,
                    $resullt
                ),
                $order
            );
        },
    );

    return $events;
}
add_filter( 'slack_get_events', 'wp_slack_woocommerce_order_status_completed2' );

1 个答案:

答案 0 :(得分:1)

要获取每日订单数量,您可以使用此自定义函数返回当天的订单计数或特定定义日期的订单计数:

function get_daily_orders_count( $date = 'now' ){
    if( $date == 'now' ){
        $date = date("Y-m-d");
        $date_string = "> '$date'";
    } else {
        $date = date("Y-m-d", strtotime( $date ));
        $date2 = date("Y-m-d", strtotime( $date ) + 86400 );
        $date_string = "BETWEEN '$date' AND '$date2'";
    }
    global $wpdb;
    $result = $wpdb->get_var( "
        SELECT DISTINCT count(p.ID) FROM {$wpdb->prefix}posts as p
        WHERE p.post_type = 'shop_order' AND p.post_date $date_string
        AND p.post_status IN ('wc-on-hold','wc-processing','wc-completed')
    " );
    return $result;
}

代码进入活动子主题(或主题)的function.php文件。经过测试并正常工作。

<强> USAGE:

1)获取当前日期的订单数:

$orders_count = get_daily_orders_count();

2)获取特定日期的订单数量(格式为2018-02-28):

// Get orders count for february 25th 2018 (for example)
$orders_count = get_daily_orders_count('2018-02-25');