0

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!

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
    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
    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
    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
    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
    Feb 15, 2017 at 11:21

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.