Change User Role After Purchasing Product

Want to change user role after purchase Automatically in WooCommerce?

If you restrict Content, Products, or other services on your site to a certain membership level, Where Users have to purchase a certain product or MemberShip plan from your WooCommerce Powered store to access it.

And,

You want to automate the process of changing User Role When an order is completed. With product id from ‘customer’ to ‘editor’ or any other membership level.

Too Easy… Just add the below code snippets in your theme’s functions.php file. ( Recommend to use Child Theme) and follow the instructions written below the snippet.

Code Snippet to change user role after purchase

add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );

function wpglorify_change_role_on_purchase( $order_id ) {

// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();

$product_id = 56; // that's a specific product ID

foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

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

            // Add role
            $user->add_role( 'editor' );
        }

}

}

Instructions:

The numbers ( 56 ) in code on line  is the ID of the products to check. Which means that snippet will check if that product purchased by the customer is in that list.

If yes, the user role will change to what you define on line 17. In this example is ( ‘subscriber’ ). To the role, we defined in line number 20 which is ‘editor’.

Automatically Complete Orders

This code works but there is one catch, You have to mark order completed manually as it’s the default behaviour of WooCommerce.

Good News is you can automate this order completion as well.

All you have to is add the following code snippets in your finctions.php file just like the previous snippet.

/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

How to find post or product ID

This Video will show you how to find product Id. The video itself is about WordPress categories but the process is the same for Products as well.

Basically, you just hover on any product in WordPress dashboard and you will see the product ID under the title of the products.

Where to add this code?

You can place PHP snippets at the bottom of your functions.php file, We recommend to use child them so you will not lose any changes when updating your theme –.

Did this snippet work?

Please let us know in the comments if snippet worked as expected. We would be happy to revise the code if you report otherwise or in case you need more assistance.

Managing Memberships

Now you have the ability to change membership levels automatically but are you struggling to manage all the memberships. Why not make things easy?

WooCommerce Memberships works out of the box it’s not only restricting your site’s content: it’s simple, site-wide membership solution that brings your content, shop, and memberships together.

You can use this in addition to the above code snippet in order to change membership levels automatically.

Watch the video below to understand the Power WooCommerce Membership Plugin

Some of the main Features (but not limited).

  • Membership plans are created independently from products.
  • Grant access to the same membership from several products (i.e., a yearly purchase or a monthly subscription)
  • Lets you schedule when your members should have access to content. For example, you can require that customers be a member for 2 days before they can access certain posts or pages.
  • Can determine which content is included in a free trial period.
  • Lets you create members-only products.
  • Add your own notes for your reference for any Member for easy communication.
  • Expiration and renewal reminder emails

12
Leave a Reply

  Subscribe  
Notify of
Bernd

Hi,

any idea of how to change the role back to trhe beginning when the member cancels subscription?

Thanks, Bernd

Kev

Thank you.
How do you change this code so that it automatically changes role if any order is made (not specific product)

Osobase

Thank for this snippet.

In the event that you do not know the current role of the user, simply make the following changes:

Remove line 17 completely……. $user->remove_role( ‘subscriber’ );

Change line 20 from:
$user->add_role( ‘editor’ );

to:

$user->set_role( ‘editor’ );

‘set_role’ will remove the previous roles of the user (whatever it is) and assign the new role.

Debashish

Great snippet. thanks for sharing. However, could you help me out if we want to check only from a particular product category and not specific product ID.

Please share the entire snippet.

Ray Dominey

This is not working for me but it is exactly what I need. Please contact me with any assistance you can provide. Thank You! get('Version') ); } add_action( 'wp_enqueue_scripts', 'child_theme_enqueue_styles' ); add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase' ); function change_role_on_purchase( $order_id ) { $order = wc_get_order( $order_id ); $items = $order->get_items(); $products_to_check = array( '178' ); foreach ( $items as $item ) { if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) { $user = new WP_User( $order->user_id ); // Change role $user->remove_role( 'customer' ); $user->add_role( 'shopper' ); // Exit the loop break; } } } ?>

akshay

thanks for sharing valuable information.