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

Instructions:

The numbers ( 55 ) 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’.

You can, of course, exsisting user role and what you want it to be.

Here is a live demo of how it works.

If you go this link https://dev-woo.wpglorify.com/product/apple-macbook-retina-display-12/ and finish the transaction (you don’t have to enter any payment details) though make sure you tick “register on site” on the checkout page.

You will notice after you complete the registration your role will be changed to Author and you will have all the author privileges.

Automatically Complete Orders

This code works but there is one catch, You have to mark order completed manually as it’s a 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 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 a 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

4 thoughts on “Change User Role After Purchasing Product”

  1. 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;
    }
    }
    }
    ?>

Leave a Comment