2

:)

I'm using wordpress with woocommerce, and I would like to automate the following step. When an order is completed, I would like to change the user role associated with that order id from 'customer' to 'subscriber'.


By searching around, I think I should be able to accomplish this by using a hook in functions.php:

add_action( 'woocommerce_order_status_completed', 'change_role_from_customer_to_subscriber' );

Then add the function:

function change_role_from_customer_to_subscriber($order_id){
// code to change role to subscriber
}

In the code, I think I need to do 2 things: 1) get the user id that is associated with that order id 2) change role of that user id to subscriber


I've tried a lot, but I couldn't get it to work (neither getting the right user id, nor changing the role of a user id). So any help would be appreciated! I've seen 2 related questions asked before on stack overflow, but unfortunately the answers there did not work for me. I hope someone can help me out!

Thanks a lot! :)



Edit: Someone helped me out with the second part of the problem, so that's great news for me :)

Unfortunately, I still haven't figured out the first part: how to get the user id that is associated with the order id. Any ideas?

4
  • Why don't you include something of what you tried so far to give us a starting point? Maybe you almost did it and there's only a small piece missing or wrong...
    – tfrommen
    Oct 30, 2013 at 14:20
  • Great idea! First thing I tried: $user = get_user_by( 'email', 'myemailaddress' ); $u = new WP_User( $user ); $u->remove_role( 'customer' ); $u->add_role( 'subscriber' );
    – Rostam
    Oct 30, 2013 at 14:30
  • Second thing I tried: $user = get_user_by_email( 'myemailaddress' ); $u= new WP_User($user); $u->set_role('subscriber');
    – Rostam
    Oct 30, 2013 at 14:33
  • Edit your question to post the code, it is really hard to read code in the comments. Oct 30, 2013 at 17:40

1 Answer 1

3

Looking through the code there is a very relevant example:

function woocommerce_paying_customer( $order_id ) {

    $order = new WC_Order( $order_id );

    if ( $order->user_id > 0 ) {

        $old_spent = absint( get_user_meta( $order->user_id, '_money_spent', true ) );
        update_user_meta( $order->user_id, '_money_spent', $old_spent + $order->order_total );

        $old_count = absint( get_user_meta( $order->user_id, '_order_count', true ) );
        update_user_meta( $order->user_id, '_order_count', $old_count + 1 );
    }
}
add_action( 'woocommerce_order_status_completed', 'woocommerce_paying_customer' );

which reminds us that the $order_is passed to the woocommerce_order_status_completed hook. From the $order_id, you can create a new order object, with the user_id as a property.

Armed with that knowledge, I think we can just fix the inner guts of the function get a new user object from that user ID, and remove the old role and finally apply the new role.

function wpa_120656_convert_paying_customer( $order_id ) {

    $order = new WC_Order( $order_id );

    if ( $order->user_id > 0 ) {
        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( 'subscriber' );
    }
}
add_action( 'woocommerce_order_status_completed', 'wpa_120656_convert_paying_customer' );
4
  • Thank you! But unfortunately, it's not working. I'm new to PHP (and coding in general), any ideas on how to debug the code and see where things go wrong?
    – Rostam
    Oct 30, 2013 at 18:11
  • Bummer. I have to admit that I didn't test it, but it seemed like a reasonable approach. There are probably better ways to debug, but I'd step through with a die('some info'); after every line... printing out the variables to see where it is failing. Oct 30, 2013 at 21:30
  • The strangest thing just happened: I was reading through my error logs, and tried your script again... and now it worked! Thank you so much, I'm very happy :)
    – Rostam
    Oct 30, 2013 at 21:58
  • Well cool! I removed the line about updating the user meta since Woo is handling that and I just forgot to delete it when copying their function. Oct 30, 2013 at 23:12

Your Answer

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

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