WooCommerce Change User Role After Purchase

June 24, 2024,
WooCommerce Change User Role After Purchase

Do you run a WooCommerce store or membership site? Want to reward your loyal customers and streamline user management? This article explores two powerful methods to automatically assign user roles based on customer purchases, creating a VIP experience that keeps them engaged! 

This article unveils two powerful methods for automatically assigning user roles based on purchases. We'll delve into both a code-based solution for developers and a user-friendly plugin option, empowering you to create a tiered membership system that fosters customer engagement and boosts sales.

Why Update User Roles After Checkout?

Automatically assigning user roles to customers after checkout can be incredibly beneficial, especially if you’re running a membership site or store. Here are some key reasons to consider this approach:

Membership Sites

For membership sites, customers often join by purchasing a membership plan, which is typically a WooCommerce product. For example, if a customer purchases your 'VIP' membership plan, they would be assigned the 'VIP Customer' user role after checkout.

Benefits Included:

Access to Restricted Content: Premium content can be accessed by VIP Customers that is not available to regular customers.
Discounts on Products: Offer discounts to VIP customers to enhance their purchasing experience and add value to their membership

Variable Membership Products

You can also create variable membership products with different tiers, such as VIP, Premium, and Basic. Each variation can correspond to a different user role, providing a flexible and tailored experience based on the membership level purchased.

Advantages of Variable Membership Products:

  • Customized User Experience: Each membership level can offer unique benefits and access, ensuring customers receive perks commensurate with their membership level.
  • Scalable Management: Easily manage and update user permissions and content access based on the assigned roles, ensuring a smooth and scalable membership system.

By integrating automatic role updates after checkout, you not only streamline the user experience but also enhance the value proposition of your membership plans, encouraging customers to upgrade and stay engaged with your site.

Method 1: Automatically Change User Role (Programmatically)

This snippet will automatically change the role of users who complete a purchase for specific products. Let me give you an example, if any one of the specified product IDs is in the order, the "Customer" role of the buyer will be replaced with the "VIP Customer" role. 

Note: You need to be careful when using this snippet to avoid giving customers the ability to edit your store!

You can modify the code so that all customers, regardless of the products bought, have a new role. This can be done by removing the if statement and foreach loop. Please note that the role is only changed when the order status is set to "Completed" (this too can be done automatically).


You'll need to add this snippet to your theme's functions.php file that will change the user role when a customer makes purchases from your store.

Here's a step-by-step guide:

Adding the Snippet to Your Theme's functions.php

  1. Log in to your WordPress admin area.
  2. Navigate to Theme Editor:
  3. Go to Appearance > Theme Editor.
  4. On the right-hand side, you will see a list of theme files. Look for functions.php (often labeled as Theme Functions).
  5. Edit functions.php:
  6. Click on functions.php to open it in the editor.
  7. Scroll to the bottom of the functions.php file.
  8. Copy and paste the provided snippet of code:

function fme_change_user_role_after_purchase($order_id) {

    $order = wc_get_order( $order_id );

    $$order_items = $order->get_items();

    $eligible_product_ids = array( '12', '19', '32', '45' ); // Change if any one of these product IDs are in the order

    foreach ($$order_items as $item) {

        if ( $order->user_id > 0 && in_array( $item['product_id'], $eligible_product_ids ) ) {

            $user = new WP_User( $order->user_id );

            $user->remove_role( 'customer' ); 

            $user->add_role( 'vip_customer' );

            break;

        }

    }

}

add_action( 'woocommerce_order_status_completed', 'fme_change_user_role_after_purchase' ); // Order Status must be Complete


Save Changes.

Method 2: Using User Roles Editor for WooCommerce

User Role Editor for WooCommerce by FME Addons lets you manage user roles for WooCommerce. You can easily Edit, Delete, and automatically change user roles based on specific conditions you apply 

Using User Role Editor for WooCommerce, you can automatically change user role when

  • User purchases a specific product
  • Order total is within the following price range
  • Customer’s total spend falls within the following price range
  • User purchases products from specific categories
  • Number of Order Exceeds
  • The amount spent in the last month is greater than

Now let's walk you through the installation, configuration, and practical application of the User Roles Editor.

Installation

  1. Download: Obtain the User Roles Editor for WooCommerce .zip file from your WooCommerce account.
  2. Upload Plugin: Navigate to WordPress Admin > Plugins > Add New and click Upload Plugin. Select the downloaded file.
  3. Install and Activate: Click Install Now, then activate the extension.

Configuration

To configure the User Roles Editor, go to WooCommerce > Settings and click the Roles Changer Button tab. You will find two tabs: Rules and Email.

Adding New User Roles

  • Go to WooCommerce > FME User Roles in the admin panel.
  • Here, you can manage all user roles. You can add new roles, and edit or delete existing ones, except for the administrator role.
  • Click the “Add Role” button to add a new user role.
  • Manage role permissions and capabilities such as Posts, Pages, Plugins, Themes, Core, Users, Files, Network, and Sites capabilities.

User Role Editor WooCommerce

Creating Rules

Rules allow you to automate user role changes based on specific conditions. Follow these steps to create rules:

Add New Rule: Enter a new rule name and click the Add New Rule button.

Specify Conditions:

  • Enable/Disable Rule: If checked, the rule will be functional.
  • Specify Action: Choose to either switch the role or gain a role.
  • From this Role: Specify the role the user is switching from (if switching roles).
  • To this Role: Specify the role the user is switching to (if switching roles).
  • Select the Role: Select the role the user is gaining (if gaining roles).

When to Apply: Set conditions for when to apply the rule, such as:

  • User purchases a specific product.
  • Order total is within a specific price range.
  • Customer’s total spend falls within a specific price range.
  • Users purchase products from specific categories.
  • Number of orders exceeds a certain amount.
  • Amount spent in the last month is greater than the specified amount.

Additional Settings:

  • Select a Product: Rules apply only to selected products.
  • Select a Price Range: Set the starting and ending price range.
  • Select Categories: Rules apply to selected categories.
  • Set a Date Range: Optionally set a date range for the rule to be applied.
  • Set Duration for Roles: Specify how many days the role will be active before reverting.
  • Switch to a Different Role: Optionally switch the user to a different role after the set duration.
  • Exclude User Roles: Exclude certain roles from the rule.

Save the Rule: Click Save to finalize the rule.

User Role Editor WooCommerce Additional Settings

Email Settings

Customize the notification emails sent to users when their role changes:

  • Sent Emails to Admin: Check to send emails to the admin.
  • Sent Email to Customer: Check to send emails to customers.
  • Email Subject: Set the email subject.
  • From Name: Set the "from" name for the email.
  • Admin Email Header Text: Set the main header text for admin emails.
  • Customer Email Header Text: Set the main header text for customer emails.
  • Header Background Color: Choose the header background color.
  • Header Text Color: Choose the header text color.


 

You can also check our detailed documentation on WooCommerce Official Site.

Additional Settings

Enable menu options to display under each menu item in Appearance > Menus > Edit Menu Item screen. This allows you to show or hide menu items based on specific user roles.

  • Show and Hide Menu to Specific User Roles: Customize menu visibility for different roles.

User role for WooCommerce

View User Roles History: 

In the admin panel, go to Users > All Users and edit any user profile to view assigned roles history and switch roles with a reason.

Benefits of Using WooCommerce User Role

Here are some additional key benefits of automatically assigning user roles based on purchases in WooCommerce:

  • Streamlined Access: Customers automatically gain access to relevant content and features based on their purchases, eliminating the need for manual enrollment or permission requests.
  • Personalized Shopping: Tailor product recommendations, discounts, and marketing messages to specific user roles, creating a more relevant and engaging shopping experience.
  • Increased Customer Satisfaction: Rewarding purchases with elevated user roles demonstrates appreciation and incentivizes repeat business.
  • Reduced Manual Workload: Automate the process of assigning user roles, freeing up your time to focus on other aspects of your store management.
  • Targeted Promotions: Utilize user roles for targeted email marketing campaigns and promotions, maximizing their effectiveness and ROI.
  • Upselling & Cross-selling: Promote relevant upsells and cross-sells to customers based on their purchase history and user role, increasing average order value.
  • Improved Customer Retention: Create a loyalty program through tiered user roles, encouraging repeat purchases and customer engagement over time.

Final Words

Empower your WooCommerce store with automatic user roles based on purchases! Reward loyal customers with exclusive access and discounts, while streamlining user management and boosting sales. This approach personalizes the shopping experience, automates tasks, and fuels customer engagement – a win-win for both you and your business!

Frequently Asked Questions

Is it safe to use the code snippet method?

Yes, but it's crucial to follow the instructions carefully and avoid modifying sections you don't understand. Consider a backup of your theme's functions.php file before making changes.

What can I do with the User Roles Editor plugin?

The WooCommerce User Role Editor plugin lets you:

  • Create new user roles for your membership system.
  • Set rules to automatically assign or change user roles based on purchase criteria.
  • Customize email notifications sent to users when their roles change.
  • Control menu visibility for different user roles.