在Woocommerce

时间:2018-04-26 09:06:35

标签: php wordpress woocommerce advanced-custom-fields orders

我正在研究一种内部“插件”,以显示我目前通过状态和客户经理分割的订单数量。所以我想把它作为一个表格来表示:

Account Manager | No of pending orders | No of canceled orders etc

我有这样一段代码:

function display_woocommerce_order_count( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'status' => 'completed',
    ), $atts );
    $statuses    = array_map( 'trim', explode( ',', $args['status'] ) );
    $order_count = 0;
    foreach ( $statuses as $status ) {
        // if we didn't get a wc- prefix, add one
        if ( 0 !== strpos( $status, 'wc-' ) ) {
            $status = 'wc-' . $status;
        }
        $order_count += wp_count_posts( 'shop_order' )->$status;
    }
    ob_start();
    echo number_format( $order_count );
    return ob_get_clean();
}
add_shortcode( 'wc_order_count', 'display_woocommerce_order_count' );

最重要的是,我看到了这段代码,但我不确定如何使用它:

public function get_customer_total_order() {
    $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' )
    ) );

    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
    }

    return $total;
}

添加了一个短代码,以便在一般订单号中显示,但没有细分给客户经理。通过ACF,我创建了一个名为"handlowiec"的自定义字段,并将其分配给订单屏幕。如何使它工作?

1 个答案:

答案 0 :(得分:4)

以下代码将按照" handlowiec" ACF字段:

enter image description here

因此,您必须将每个订单分配给客户经理ID:

enter image description here

具有非常轻的SQL查询的函数代码(基于wp_count_posts() WordPress函数)

function display_woocommerce_order_count( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'status' => 'completed, processing, on-hold, cancelled',
        'handlowiec' => ''
    ), $atts );

    // Formatting the order statuses for the SQL query
    $statuses = $data = [];
    $statuses_array = array_map( 'trim', explode( ',', $args['status'] ) );

    foreach ( $statuses_array as $status ) {
        if ( 0 !== strpos( $status, 'wc-' ) )
            $statuses[] = 'wc-' . $status;
    }
    $statuses = implode("','", $statuses);

    ## -- 1. The SQL Query -- ##

    global $wpdb;
    $handlowiec_query = $join_postmeta = $output = '';

    // Handling the Account Manager ID (optionally)
    if( $args['handlowiec'] !== '' ){
        $join_postmeta = "INNER JOIN {$wpdb->prefix}postmeta pm ON p.ID = pm.post_id";
        $handlowiec_query = "AND meta_key like 'handlowiec' AND meta_value = ";
        $handlowiec_query .= $args['handlowiec'];
    }

    $results = $wpdb->get_results("
        SELECT post_status, COUNT( * ) AS num_posts
        FROM {$wpdb->prefix}posts as p
        $join_postmeta
        WHERE post_type = 'shop_order'
        AND post_status IN ('$statuses')
        $handlowiec_query
        GROUP BY post_status
    ");

    ## -- 2. Formatting the Output -- ##

    // Loop through each order status count
    foreach($results as $result){
        $status = str_replace( 'wc-', '', $result->post_status );
        $orders_count = (int) $result->num_posts;

        if( $orders_count > 0 )
            $data[] =  ucfirst($status) . ' ' . _n( 'order', 'orders', $orders_count ) . ': ' . $orders_count;
    }
    if( $args['handlowiec'] !== '' )
        $output = 'Account Manager ID ' . $args['handlowiec'] . ' | ';
    else
        $output = 'All Account Managers | ';

    if( sizeof($data) > 0 )
        $output .= implode( ' | ', $data );
    else
        $output .= 'No results';

    return '<p>' . $output . '</p>';
}
add_shortcode( 'wc_order_count', 'display_woocommerce_order_count' );

代码放在活动子主题(或活动主题)的function.php文件中。经过测试和工作。

用法:

1)没有&#34;客户经理ID&#34; (对所有经理人):

 echo do_shortcode("[wc_order_count]");
  

你会得到类似的东西:

All Account Managers | On-hold orders: 2 | Processing orders: 7 | …

2)使用特定的&#34;客户经理ID&#34;:

echo do_shortcode("[wc_order_count handlowiec='5']");
  

你会得到类似的东西:

Account Manager ID 5 | On-hold order: 1 | Processing orders: 3 | …

您还可以使用status之前的参数来指定输出中涉及哪些订单状态...