簡體   English   中英

Woocommerce通過用戶訂單總數更改角色

[英]Woocommerce change role by total of user orders

我對自動更改用戶角色的php實現有疑問。 所以我有自定義角色dovclient 首次用戶訂購獲得角色Customer

如果他購買的產品總額為500 $,我需要更改用戶角色。

所以我有一個功能

function wpa_120656_convert_paying_customer( $order_id ) {

  $order = new WC_Order( $order_id );

    $user_id = $order->user_id;

    $customer_orders = get_posts( array(
      'numberposts' => -1,
        'meta_key'    => '_customer_user',
      'meta_value'  => $user_id,
        'post_type'   => 'shop_order',
    ) );

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

    return $total;

    if ( $total > 500 ) {
       update_user_meta( $order->user_id, 'paying_customer', 1 );
        $user = new WP_User( $order->user_id );


        $user->remove_role( 'customer' ); 


       $user->add_role( 'dovclient10' );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );

我知道, $customer_orders正在計數,並且我使功能按訂單計數工作。 我的意思是,例如,如果$customer_orders > 2 ,則更改角色。

但是總的訂單價格我有一個問題。 角色不會改變。 我不知道為什么 也許問題在foreach ...

請幫我!

很棒的幫助! 0 ...好 我已經做了。

function wpa_120656_convert_paying_customer( $order_id ) {

  $order = new WC_Order( $order_id );

    $user_id = $order->user_id;

    $customer_orders = get_posts( array(
      'numberposts' => -1,
        'meta_key'    => '_customer_user',
      'meta_value'  => $user_id,
        'post_type'   => 'shop_order',
    ) );

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

    if ( $totall > 500 ) {
        update_user_meta( $order->user_id, 'paying_customer', 1 );
       $user = new WP_User( $order->user_id );

        if( get_role('customer') ){
             $user->remove_role( 'customer' ); 
             $user->add_role( 'dovclient' );
        }
   }
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM