2017-12-02 21 views
1

お客様がキャンセルした注文数を数え、管理注文画面に表示しようとしています。Woocommerce管理者の注文編集ページでお客様のキャンセル注文数を表示

私の問題は、遠隔地の顧客のために働くことができないということです。自分自身で(current_userとして)働くことができます。

これは私のコードである(他のグーグルといくつかの小さな修正から取った):

function count_order_no($atts, $content = null) { 
$args = shortcode_atts(array(
    'status' => 'cancelled', 
), $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', 'count_order_no'); 

、その後、すべてのヘルプは非常にapreciatedさ

// print the number 
function print_the_number() { 
echo do_shortcode('[wc_order_count]'); 
} 

// add the action 
add_action('woocommerce_admin_order_data_after_order_details', 'print_the_number', 10, 1); 

adminに番号を表示!

答えて

0

お客様IDを現在の注文からターゲット設定する必要があります。はるかに簡単な方法で行うことができます。

あなたはこれを試してみてください:

add_action('woocommerce_admin_order_data_after_order_details', 'get_specific_customer_orders', 10, 1); 
function get_specific_customer_orders($order) { 

    $customer_orders = get_posts(array(
     'numberposts' => -1, 
     'meta_key' => '_customer_user', 
     'meta_value' => $order->get_customer_id(), 
     'post_type' => 'shop_order', 
     'post_status' => array('wc-cancelled'), 
    )); 

    $orders_count = '<strong style="color:#ca4a1f">' . count($customer_orders) . '</strong>'; 

    echo'<br clear="all"> 
    <p>' . __('Cancelled orders count: ') . $orders_count . '</p>'; 
} 

コードはfunction.phpのあなたのアクティブな子テーマのファイル(またはアクティブテーマ)または任意のプラグインファイルになります。

テスト済みで動作します。

+1

ありがとう、魅力的な作品です!確かにあなたの方法ははるかに簡単で明確です! – RwkY

関連する問題