繁体   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