programing

Woocommerce에서 상태별 및 사용자 정의 ACF 필드별 주문 수 표시

starjava 2023. 7. 11. 21:24
반응형

Woocommerce에서 상태별 및 사용자 정의 ACF 필드별 주문 수 표시

저는 현재 상태와 계정 관리자별로 분할된 주문이 얼마나 있는지 보여주기 위해 일종의 내부 '플러그인' 작업을 하고 있습니다.그래서 다음과 같은 표로 제시하고자 합니다.

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"주문 화면에 할당했습니다.어떻게 작동합니까?

다음 코드는 "handlowiec" ACF 필드에 해당하는 현재 계정 관리자 ID에 대한 상태별 주문을 계산합니다.

enter image description here

따라서 각 주문에 계정 관리자 ID를 할당해야 합니다.

enter image description here

매우 가벼운 SQL 쿼리가 있는 함수 코드(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' );

코드가 작동합니다.활성 하위 테마(또는 활성 테마)의 php 파일입니다.테스트를 거쳐 작동합니다.


용도:

계정 관리자 ID가 없는 경우(모든 관리자용):

 echo do_shortcode("[wc_order_count]");

다음과 같은 이점을 얻을 수 있습니다.

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

특정 "계정 관리자 ID" 사용:

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

다음과 같은 이점을 얻을 수 있습니다.

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

이전과 같이 사용할 수도 있습니다.status출력에 포함될 주문 상태를 지정하는 인수...

언급URL : https://stackoverflow.com/questions/50039028/show-number-of-orders-by-statuses-and-by-custom-acf-field-in-woocommerce

반응형