2017-12-13 19 views
1

異なる住所への出荷が使用された場合、woocommerceで注文の注文ステータスを変更したいと考えています。& Woocommerceは、顧客が以前に試したことがある可能性があるため、失敗した過去の注文を区別する必要があります。これは、システムに注文がありますが、失敗、キャンセルまたは保留になります。新規顧客がWooCommerceの別の住所に出荷された場合の注文ステータスの変更

「確認」というシステムで新しい注文ステータスをすでに作成しました。「別の住所に出荷」オプションを使用して注文を行うとスクリプトを実行するようになりました。前に注文したかどうかそうでない場合は、注文ステータスを"確認"に変更します。

私が最初に試したのは、注文が確認されるまで出荷されないという注文が1つしかない場合、顧客に以下のコードで警告を出すことでした。しかし、どれだけの注文があっても表示されます。

私が試してみましたがfunctions.phpファイルにこのコードを設定している:彼らは別のアドレスとその彼らの最初の注文に船を選択した場合、私は行方不明です何

function wc_get_customer_orders() { 

// Get all customer orders 
$customer_orders = get_posts(array(
    'numberposts' => 1, 
    'meta_key' => '_customer_user', 
    'meta_value' => get_current_user_id(), 
    'post_type' => wc_get_order_types(), 
    'post_status' => array_keys(wc_get_order_statuses()), 
)); 

$customer = wp_get_current_user(); 

// Text for our message 
$notice_text = sprintf('Hey %1$s 😀 As this is your first order with us, we will need to verify some info before shipping.', $customer->display_name); 

// Display our notice if the customer has no orders 
if (count($customer_orders) == 1) { 
    wc_print_notice($notice_text, 'notice'); 
} 
} 
add_action('woocommerce_before_my_account', 'wc_get_customer_orders'); 

だけでこのアラートをトリガーする方法です。

また、検証するためにステータスを更新するコードもそこでトリガされる必要があります。

ご協力いただければ幸いです。

答えて

2

これは、ページ(ありがとうページ)注文はを受けた」WooCommerce上で最初に行われるとは限られています。

新規顧客で、請求と発送の詳細の差異が検出された場合、注文ステータスが変更され、お客様のアカウントページにリンクされたボタン付きのカスタム通知が表示されます。

私のアカウントページには、を別様にカスタマイズすることができる同様の通知が表示されます(または、代わりに指示にコンテンツにテキストを挿入できます)

私は、フックされた両方の機能で顧客の注文をカウントする分離機能を作成しました。

WooCommerceには専用のカウント機能wc_get_customer_order_count()がありますが、この場合は便利ではありません。

コード:

// Counting customer non failed orders (light sql query) 
function get_customer_orders_action($user_id, $status = true){ 
    global $wpdb; 

    // if argument $status is set to "false" we check for status 'wc-verify' instead 
    $status_arg = $status ? "NOT LIKE 'wc-failed'" : "LIKE 'wc-verify'"; 

    // The light SQL query that count valid orders (no failed orders in count) 
    $result = $wpdb->get_col(" 
     SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts as p 
     INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id 
     WHERE p.post_type LIKE '%shop_order%' AND p.post_status $status_arg 
     AND pm.meta_key LIKE '_customer_user' AND pm.meta_value = $user_id 
    "); 

    return reset($result); 
} 

// Conditionally Changing the order status and displaying a custom notice based on orders count 
add_action('woocommerce_thankyou', 'new_customer_shipping_verification', 15, 1); 
function new_customer_shipping_verification($order_id){ 
    $order = wc_get_order($order_id); // Get the order OBJECT 

    // CHECK 1 - Only if customer has only 1 Order (the current one) 
    if(get_customer_orders_action($order->get_user_id()) != 1) 
     return; // we exit 

    // Get some shipping and billing details to compare 
    $b_firstname = $order->get_billing_first_name(); 
    $s_firstname = $order->get_shipping_first_name(); 
    $b_address1 = $order->get_billing_address_1(); 
    $s_address1 = $order->get_shipping_address_1(); 

    // CHECK 2 - Only if there is a difference beetween shipping and billing details 
    if($b_firstname == $s_firstname && $b_address1 == $s_address1) 
     return;// we exit 

    ## --- --- Now we can update status and display notice --- --- ## 

    // Change order status 
    $order->update_status('verify'); 

    // The complete billing name 
    $user_name = $order->get_billing_first_name().' '; 
    $user_name .= $order->get_billing_last_name(); 

    // The text message 
    $text = __('Hey %s %s As this is your first order with us, we will need to verify some info before shipping.'); 
    $message = sprintf($text, $user_name, '😀'); 
    // The button and the link 
    $link = esc_url(wc_get_page_permalink('myaccount')); 
    $button = '<a href="'.$link.'" class="button" style=float:right;>'.__('Check your info').'</a>'; 

    // Display the custom notice 
    wc_print_notice($message.$button, 'notice'); 
} 

// Conditionally Displaying a custom notice in my account pages 
add_action('woocommerce_account_content', 'my_account_shipping_verification', 2); 
function my_account_shipping_verification(){ 
    // Get the current user ID 
    $user_id = get_current_user_id(); 
    $user_data = get_userdata($user_id); 

    // Only if customer has almost an Order with status like 'verify' 
    if(get_customer_orders_action($user_id, false) == 0) 
     return; // we exit 

     // The complete billing name 
     $user_name = $user_data->first_name.' '; 
     $user_name .= $user_data->last_name; 

     // The text message (to be completed) 
     $text = __('Hey %s %s As this is your first order with us, we will need to...'); 
     $message = sprintf($text, $user_name, '&#x1f600;'); 

     // Display the custom notice (or it can be a normal text) 
     wc_print_notice($message, 'notice'); 
    } 
} 

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

試験済みとなります。

+0

ありがとうございました。 このコードをテストすると、次の致命的なエラーが発生しました。 致命的なエラー:/ home/******/public_html/wp-content/themes/*******/functionsのブール値でメンバ関数get_user_id()を呼び出します。php on line 1422 – 305David

+0

@ 305David ...私は答えを更新しました...それは少し間違いでした。もう一度やり直してください。今回はうまくいくはずです。おかげで – LoicTheAztec

+0

ありがとうLoicは、このWordPressフォーラムがあなたのサポート外であることを知っていません。 このコードが動作することを確認できます。 船舶を異なる住所に選んだ新規顧客からの注文を確認し、ステータスを確認するように設定します。 私は手動で、管理注文画面のステータスボタンの幅と高さを広げ、また赤い背景を与えるクラス "確認"にいくつかのCSSを手動で追加しました。店の​​管理者は、検証する必要があります。 もう一度ありがとうございます。 – 305David

関連する問題