Support » Plugin: User Role Editor » Multiple User Roles for Woocommerce on Purchase

  • When a customer purchases a product through the Woocommerce interface, a user record is added and a role is created called “Customer”. So far so good.

    However, depending on what the customer purchased, we want to add a 2nd user role. i.e. if a customer purchase a car we would want the user role to be “customer, car”

    If the customer purchased a boat, it would be “customer, boat”.

    Prefer this to be done automatically when a user buys a specific product.

    Is this possible?

    Thanks.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author Vladimir Garagulia

    (@shinephp)

    You may hook your code to woocommerce_created_customer action and add needed role to a new created user. Woocommerce code reference: inludes/wc-user-functions.php, function wc_create_new_customer():

    
    do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated );
    

    I need the same thing! Can you give us an answer for dummies please?

    I would take this to mean that we must edit the “inludes/wc-user-functions.php” file and add the code snippet somewhere in there. But where exactly to add it? AND where do we define the role name in this code snippet? And how do we tie this to a specific product?

    • This reply was modified 2 months, 3 weeks ago by earth_human.

    This worked for me. Make sure to add your product IDs in line 6 and user roles in line 13 and 14. https://stackoverflow.com/questions/44770912/woocommerce-change-user-role-on-purchase

    To add multiple product ID’s, line 6 would look like this:
    $products_to_check = array( '1288', '1495', '1496', '1497' );

    • This reply was modified 2 months, 3 weeks ago by earth_human.

    Thanks earth_human. Looks promising and will try it.

    Did you confirm where the code is entered i.e. wc-user-functions

    And, if woocommerce updates their plugin, does this mean that this code would have to be re-added after update. Not quite sure how that would work.

    Thanks,

    Doesn’t seem to be working. Here is what I have:

    function change_role_on_purchase( $order_id ) {

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

    foreach ( $items as $item ) {
    $product_name = $item[‘name’];
    $product_id = $item[‘product_id’];
    $product_variation_id = $item[‘variation_id’];

    if ( $order->user_id > 0 && $product_id == ‘3782’ ) {
    update_user_meta( $order->user_id, ‘customer’, 1 );
    $user = new WP_User( $order->user_id );

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

    // Add role
    $user->add_role( ‘customer, roadmap’ );
    }
    }
    }

    add_action( ‘woocommerce_order_status_completed’, ‘change_role_on_purchase’ );

    I am putting the php file in the wp_includes folder. Does the file need to go into a sub-folder?

    When the order is processed, it goes to “On Hold” and then I have to change it manually to completed. Not sure if this is the issue or not.

    Any comments?

    Thanks

    Plugin Author Vladimir Garagulia

    (@shinephp)

    1st,
    WP_User::add_role(); can add 1 role for a time. You have to use:

    
    $user->add_role( 'customer' );
    $user->add_role( 'roadmap' );
    

    2nd, insert code to your active theme functions.php file or set it as a Must Use plugin. Do not touch woocommerce files. I reference one as a description for action which you can use for your code.

    Thank you Vladimir. Can you tell me how I set the php file as a Must Use.

    Plugin Author Vladimir Garagulia

    (@shinephp)

    Finding this a bit frustrating. Your advice is great but still can’t get it to work.

    So, I created a folder mu-plugins in wp-content. And added my php file to it with the code.

    Two things happen: First, every page I click on has the function code displayed across the top.

    Second, on checkout, I can enter the customer information, but cannot proceed past that. All the pay buttons are blanked out.

    Do you know of a step-by-step process I can follow.

    Thanks,

    Plugin Author Vladimir Garagulia

    (@shinephp)

    1st of all, insert into your .php file <? characters as the very 1st separate line.

    Thanks Vladimir. All is working great. Very much appreciated all your help on this.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.