Skip Navigation

[Resolved] WooCommerce Subscriptions and Toolset Access – multiple role issue

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

Author
Posts
#2040889
Screenshot 2021-05-03 at 13.18.18.png
Screenshot 2021-05-03 at 13.22.24.png
Screenshot 2021-05-03 at 13.26.35.png

Dear Support

I've been browsing through the forum and found some articles but they are very old and want an up-to-date clarification of how Toolset Access is expected to work with WooCommerce Subscriptions.

Recently we noticed that WooCommerce Subscriptions is not updating the user role correctly once a subscription has been processed. When the payment is processed, the new role is being ADDED to the user instead of replacing it.

On our site, a user begins with the role "Family". When they subscribe, their role should change to "Subscribed Family" and if they cancel their subscription or it expires, it should change to "Lapsed Family", as specified under
WooCommerce > Settings > Subscriptions > Subscriber Default Role and
WooCommerce > Settings > Subscriptions > Inactive Subscriber Role.

However, when subscribing, the role "Subscribed Family" is being ADDED to the user, so it has two two roles: "Family" and "Subscribed Family". When cancelled, the user has "Family" and "Lapsed Family".

I believe that Subscriptions plugin should REPLACE the user role with that specified under
WooCommerce > Settings > Subscriptions > Subscriber Default Role and
WooCommerce > Settings > Subscriptions > Inactive Subscriber Role

This worked fine previously in that the user's role was replaced with Subscribed Family instead of adding it.

I contacted WooSupport and they advised me to check with you. Has something changed with the latest plugin releases in this regard? Do I need to change any default settings for Subscriptions under Toolset Access settings? Currently Subscriptions is not being managed by Access on our site and has the default WordPress read permissions.

Kind regards
Simon

#2041413

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon and thank you for contacting the Toolset support.

It seems that WooCommerce subscriptions do not support users with multiple roles. Somehow, it manages to add/remove the roles that are configured on it, but the additional roles are not touched. You can confirm that by testing with a Family user that has additional roles(Subscriber, Customer, Contributor), I suspect that WooCommerce Subscriptions will not touch any of these roles.

WooCommerce Subscriptions offers a filter that will be triggered when it changes the user's roles based on a change of a subscription. The hook is "woocommerce_subscriptions_updated_users_role", you can read more about it at the bottom of this page https://docs.woocommerce.com/document/subscriptions/develop/action-reference/

You can hook into this action and change the user roles based on the $role_new or the $role_old arguments.
- if $role_new = Subscribed Family or $role_new= Lapsed Family: Remove the role "Family" and let WooCommerce subscription set the role configured for this status of the subscription.

$user->remove_role( 'Family' );

I hope this makes sense. Let me know if you have any questions.

#2045141

HI Jamal

I'm not a PHP programmer, but I had a go at what you suggested. Here's my attempt:

add_action( 'woocommerce_subscriptions_updated_users_role', 'func_remove_family_role' );

function func_remove_family_role() {

	$user = wp_get_current_user();
	
	if ( $role_new = 'lapsed_family' && $role_old = 'subscribed_family' ) {
		$user->remove_role( 'family' );
		} else if ( $role_new = 'subscribed_family' && $role_old = 'family' ) {
			$user->remove_role( 'family' );
		}
}

It didn't work unfortunately, but is also not throwing any errors that I can see.

I tried using the "internal" names for the roles as well as was done in other custom codes provided by Toolset for us in the past, but neither worked.

The scenarios in English would be:
1) get the current user
2a) if the user subscribes successfully, switch the user's role from Family (family) to Subscribed Family (subscribed_family)
else
2b) if user cancels their subscription or it expires, change their role from Subscribed Family (subscribed_family) to Lapsed Family (lapsed_family)
else do nothing

Could you cast your expert eye over my code and make some improvements?

Kind regards
Simon

#2045143

HI Jamal

Having looked at the code after I posted, I think it could be simplified further.

We only need to change it from Family to Subscribed Family initially and kill Family, I think. Once it's already Subscribed Family, Woo seems to manage changing "Subscribed Family" to "Lapsed Family" correctly already, so Family should no longer be available anyway, right?

Kind regards
Simon

#2047649

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

You had some simple errors with the code and the condition logic. First, you need to add the priority and the number of arguments in line 1. And you need to add the arguments in line 3. Then you need to adapt the logic, we'll check for the $new_role, if it is "Subscribed Family " or " Lapsed Family", we'll remove the "Family" role. You should also use the role name instead of a slug:

add_action( 'woocommerce_subscriptions_updated_users_role', 'func_remove_family_role', 10, 3 );
 
function func_remove_family_role($new_role, $user, $role_old) {
 
    if ( $role_new == 'Lapsed Family' || $role_old == 'Subscribed Family' ) {
        $user->remove_role( 'family' );
    }
}

If this does not work for you, please let me know how to test it and I'll give it a try(how do you change the subscription to test? Do you wait for automatic subscription change? Or do you trigger it manually?)

#2047921

Hi Jamal

Thanks for the corrections. 😊
Line 6: I changed it to "Family" from lower case "family".

1) I tried this out and it didn't work unfortunately.
2) What would you expect to happen if the role Family didn't exist any more for a user? Would it error out?

To answer your question, when subscribing the first time, ie when their role should change from "Family" to "Subscribed Family", the Order should auto-complete and then switch the user to Subscribed Family role and the user should get the Active Subscriber tick in WP Admin > Users.

When users cancel, it appears that this process goes to a Pending Cancellation status in Woo, where the Subscription remains in this state until the Subscription end date, or until manually pushed through to full cancellation in the WP Admin > Subscriptions screen.

During this Pending cancellation stage, the user sees a "Reactivate" button on their Woo My Account > Subscriptions page (the shopping trolley icon in our menu). When the subscription is forced cancelled in WP Admin > Subscriptions, and changes from status "Pending Cancellation" to "Cancelled", the user then sees a "Resubscribe" button.

Only when the order status transitions to "Cancelled" should the user role change from "Subscribed Family" to "Lapsed Family", since it could be that they wish to cancel their automatic renewal of their subscription, however they have paid for, say, 3 months, and should still have all the premium benefits of being a Subscribed Family right up to the Subscription end date.

It might be best to create a dummy family user for yourself, which you can delete and recreate as many times as you wish:
1) Click Sign Up in the menu
2) Sign up as a Family
3) Click Find a Native Nanny, click on View Ad to view the full Ad, there you should be able to subscribe - the shop is in test mode, so you can just use the fake credit card number specified in the shop and subscribe yourself and view the status changes in WP Admin Users and WP Admin > WooCommerce > Subscriptions. To view your Woo My Account page, click the shopping trolley in the menu bar.

Kind regards
Simon

#2048109

Hi Jamal

Sorry one last thing, if you create yourself as a Family as described by me earlier, you will have to go into WP Admin > Users in the back end and changed your role manually from Unverified Family to Family so you have access to all the pages you need.

Kind regards
Simon

#2048113

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Thank you Simon! I still need to know how the subscription progresses, so I can check if the custom code will be triggered and how it will behave.

Once I create a subscription for my new user, do I need to do something in the backend, or the subscription will complete successfully and will add the role "Subscribed Family" to the user?

To check how again for the cancelation or the expiration of the subscription, should I do something in the backend?

Can you also provide FTP access to your website, so I can revert it if it crashes because of an error in the custom code? Your next reply will be private to let you share credentials safely.

#2048235
#2050233

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Simon,

I worked on this many times during the weekend, but I can't get the code to run. Once it was running and giving issues because of the SMTP service, but after deactivating all of it, it still does not trigger the hook when I create a subscription for the user.

I need to take a copy of your website and debug it locally. Would you allow me to take the copy?

#2050255

HI Jamal

Of course you can! Please let me know if you have any issues or need any further assistance.

Kind regards
Simon

#2050261

Hi Jamal

We were receiving these errors:

5.1.2 <test2@example.com>: Recipient address rejected: Malformed DNS server reply

This seems to be the error you were talking about, in case that is relevant for you. If you prefer to use one of our fake user accounts, please let me know. They have a fully working email address.

Kind regards
Simon

#2050379

Hi Jamal

Something occurred to me which might be the reason why you are not seeing things working as you might expect after changing some code. We recently introduced WP Rocket on our site, on the recommendation from Dario from WPML, and I noticed that I had to clear the cache after making changes to Views and other things to be able to see the changes correctly on the front end. In the Admin menu area, there is a WP Rocket item now, I have been using WP Rocket > Clear Cache > All languages to ensure that nothing is cached after I make any changes in WP Admin.

I'm not sure if that helps, but maybe it does! 😉

Best regards
Simon

#2050665

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Maybe I just needed to give it a rest. I was able to trigger the code and error logs this morning, and it turns out to use role slugs instead of name. The following code should work to remove the Family role when the subscription is active or canceled.

add_action( 'woocommerce_subscriptions_updated_users_role', 'func_remove_family_role', 10, 3 );
  
function func_remove_family_role($new_role, $user, $role_old) {
    if ( $new_role == 'subscribed_family' || $new_role == 'lapsed_family' ) {
        $user->remove_role( 'family' );
    }
}

Please note that I disabled some plugins, WP Rocket included, during my tests.

Please test it from your side and let me know if you still need assistance with this.

#2050825

Hey Jamal

It appears to be working great now!

The debug.log is from you, right? And you changed our wp-config to enable it? I can now delete that and your two test users. Right?

Kind regards
Simon

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.