Hajira Nayyab
Hajira Nayyab

Reputation: 11

Assign a Custom User Role Upon WooCommerce Subscription Activation

In WooCommerce, I need to automatically assign a custom role ("Active Production Company") to users upon their subscription becoming active. The role assignment will allow only active subscribers to access and submit a restricted form on the site, created with WP Job Manager.

I’ve added a WooCommerce subscription product (monthly and yearly) for users to purchase, which grants them the custom role upon successful payment.

Here’s the code I’m currently using:

add_action( 'woocommerce_subscription_status_updated', 'wc_subscribe_assign_role', 10, 3 );

function wc_subscribe_assign_role( $subscription, $new_status, $old_status ) {
   if ( $new_status === 'active' ) {
       $user_id = $subscription->get_user_id(); // Get the user ID of the subscriber
       $user = new WP_User( $user_id );
        
        // Assign the "Active Production Company" role upon subscription activation
        $user->set_role( 'active_production_company' );
    }
}

I’m not certain if $subscription is being used correctly to get the user ID, or if woocommerce_subscription_status_updated is the best hook for this purpose.

I used the woocommerce_subscription_status_updated hook, hoping it would trigger role assignment upon a subscription’s status change to "active." I expected this to automatically assign the "Active Production Company" role to users, allowing them to access the restricted form.

After testing with a test order, the expected role was not applied. I checked the WooCommerce subscription object documentation and my error logs but couldn’t confirm if the $subscription->get_user_id() part was working as intended. I’d appreciate any insights or suggestions for achieving this functionality reliably!

Upvotes: 1

Views: 117

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254362

Note that the WC_Subscription Object inherits methods from WC_Abstract_Order and WC_Order classes, so you can use either:

  • get_user() method, to get the WP_User object,
  • get_user_id() method, to get the user ID.

Now you can better use woocommerce_subscription_status_active composite filter hook, that is triggered when a subscription become active.

Note that you should add your custom role instead of replacing the current user role(s), as a subscriber role is assigned and used by WooCommerce Subscriptions plugin.

Here is a simplified code version:

// Add "Active Production Company" role upon subscription activation
add_action( 'woocommerce_subscription_status_active', 'add_role_to_active_subscribers', 10 );
function add_role_to_active_subscribers( $subscription ) {
    $custom_role =  'active_production_company'; // Custom user role to add
    $user = $subscription->get_user(); // Get the WP User object
 
    // If user has not yet the custom user role
    if( is_a( $user, 'WP_User' ) && ! in_array(  $custom_role, (array) $user->roles ) ) {
        $user->add_role( $custom_role ); // Add custom role
    }
}

It should work.

If you really wish, you can always use set_role() method instead of add_role().

Upvotes: 0

Related Questions