admin管理员组

文章数量:1122832

According this answer i want to count orders of each user, and if count > 3 for example, change role to another.

After searching i found this hook

I have modified function

function wpa_120656_convert_paying_customer( $order_id ) {

    $order = new WC_Order( $order_id );

    $int = wc_get_customer_order_count( $user_id );
    
    if ( $int->user_id > 2 ) {
        update_user_meta( $order->user_id, 'paying_customer', 1 );
        $user = new WP_User( $order->user_id );

        // Remove role
        $user->remove_role( 'customer' ); 

        // Add role
        $user->add_role( 'dovclient' );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );

But it doesn't work... Help me to find a mistake, please!

According this answer i want to count orders of each user, and if count > 3 for example, change role to another.

After searching i found this hook

I have modified function

function wpa_120656_convert_paying_customer( $order_id ) {

    $order = new WC_Order( $order_id );

    $int = wc_get_customer_order_count( $user_id );
    
    if ( $int->user_id > 2 ) {
        update_user_meta( $order->user_id, 'paying_customer', 1 );
        $user = new WP_User( $order->user_id );

        // Remove role
        $user->remove_role( 'customer' ); 

        // Add role
        $user->add_role( 'dovclient' );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );

But it doesn't work... Help me to find a mistake, please!

Share Improve this question edited Aug 19, 2024 at 6:54 Cow 1111 silver badge5 bronze badges asked Feb 15, 2017 at 10:52 Alex PainAlex Pain 34 bronze badges 6
  • You want to do it for all your current customers that had already orders > 3 or from now, on every time a customer pass the threshold of 3 orders change the role? – Laxmana Commented Feb 15, 2017 at 11:05
  • @Laxmana Now my site is testing, and i think the 2 variant pass for me. This function catch new order, catch user id, if user id has 3 or more orders, change status. – Alex Pain Commented Feb 15, 2017 at 11:09
  • Ok and why isn't working? What is the problem? Have you test if the action is being fired? – Laxmana Commented Feb 15, 2017 at 11:12
  • @Laxmana This function doesn't work, i don't know why...may be $int = wc_get_customer_order_count( $user_id ); call not right.... – Alex Pain Commented Feb 15, 2017 at 11:16
  • First of all you have to see if the function is being executed, meaning that wordpress is calling the function. You can do this by echo/print_r/error_log inside the function. Secondly check the content of the $int variable by var_dump or print_r – Laxmana Commented Feb 15, 2017 at 11:21
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Thank you for all people that help me to find an answer (0 helpers)! I found a solution on my own.

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',
    ) );

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

        // Remove role
        $user->remove_role( 'customer' ); 

        // Add role
        $user->add_role( 'dovclient' );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' ); 

本文标签: hooksChange user role if it39s orders count more than