Skip Navigation

[Resolved] How do you integrate Woocommerce Susbscription to Toolset Access

This support ticket is created 6 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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: Asia/Karachi (GMT+05:00)

Author
Posts
#1122782

Hi,

A little background on my project - I’m building a Directory site for bars.

hidden link

The bar owners can choose 1 of 3 different pricing/packages to choose from and become members with recurring payment subscriptions. Depending on the package they will have access to different functionality with is listed on the Pricing page:

hidden link

Free - not on the pricing page yet, but basically is just when a bar owner registers and claims their business and gets a Verified Business badge.

Package 1(paid) - can edit specific parts of their listing (hours, website url, FB url, etc.)

Package 2(paid) - can do all of package 1 plus adding coupons and events

Package 3(paid) - can do all of package 2 plus claim/manage multiple listings plus “Featured” listing status which highlights their listing and moves it to the top of the homepage, category and search pages.

I have built the back-end dashboard for the members to be able to edit listings and create/edit/delete coupons. My question is, at this point once they click a package to become a member what is the best process to enroll them in the membership, allow them the specific features their package offers, and provide the ability for things like upgrading/downgrading.

I’ve already started integrating WooCommerce and Woo Subscriptions to handle the registration and subscriptions. And I have Toolset Access installed as I hope this will allow me to show/hide content and functionality.

I guess a few more specific questions would be:
1) I imagine they will have to have roles assigned to them depending on what package they choose when they register. How can I assign roles based on the package they choose?

2) How do I limit/offer the different functionality the different packages/roles have (for example Package 1 will not have access to the dashboard tab for adding/editing coupons, and package 3 will allow bar owners to claim multiple listings and manage these in the dashboard)

3) Listings that are Featured, so members who have Package 3, have a featured banner, appear on the Homepage, and appear at the top of Category pages. Is this something I can do with a View?

Thanks for your assistance.

#1123480

Hi Timothy,

Thank you for contacting us and I'll be happy to assist.

You can use WooCommerce, Woo Subscriptions, and Toolset Access plugins together, for a membership/directory website with a single user level (free or paid), out-of-the-box.

But for multiple user levels ( i.e. basic free, silver, gold, and platinum ), you'll either need their "WooCommerce Memberships
" plugin ( https://woocommerce.com/products/woocommerce-memberships/ ) or some custom code that can change the user roles based on status of subscription levels for different products/subscriptions.

Here is a useful guide on how WooCommerce Subscriptions and the Toolset plugin can be used togather:
https://toolset.com/learn/create-membership-site-wordpress-using-toolset-plugins/how-to-use-the-woocommerce-subscriptions-plugin-with-toolset/

Toolset Access plugin offers features to create custom user roles, manage existing roles & their capabilities and controlling access to website's content based on user roles. But it doesn't include option to assign different roles to users based on the products or active subscriptions they have in their account.

As for your specific questions:

1) I imagine they will have to have roles assigned to them depending on what package they choose when they register. How can I assign roles based on the package they choose?

- Your understanding is correct and each user level or role will need to have its own subscription product, that members can pay for to get that access.

But, you'll need "WooCommerce Memberships" extension or some custom code that makes use of WooCommerce Subscription plugin's API ( https://docs.woocommerce.com/document/subscriptions/develop/ ), to change roles for individual products (as it offers only global control for all products).

2) How do I limit/offer the different functionality the different packages/roles have (for example Package 1 will not have access to the dashboard tab for adding/editing coupons, and package 3 will allow bar owners to claim multiple listings and manage these in the dashboard)

- Once you've managed to assign different roles to different group of users, the Toolset Access plugin will allow you full control to show/hide specific content sections, based on their user roles:

https://toolset.com/documentation/user-guides/limiting-read-access-specific-content/
https://toolset.com/documentation/user-guides/access-control-texts-inside-page-content/

3) Listings that are Featured, so members who have Package 3, have a featured banner, appear on the Homepage, and appear at the top of Category pages. Is this something I can do with a View?

- Yes, using the Toolset Views plugin, you'll be able to create a view that shows featured content/banner (post types) filtered by author's user level.

I hope this helps! Please let us know if you have any further questions.

regards,
Waqar

#1124275

Hi Waqar,

Thank you for the detailed response. I suspected I might need WooCommerce Memberships. But I'd prefer not to use that if the custom code to change roles isn't too complicated. I know you can't offer that, but can you give me any direction with how I would begin that custom code and how involved it might be? I'd just like to weigh the options (the extra weight and complexity of adding WooCommerce Memberships vs. difficulty/cost to create the custom code).

I wonder if another option might be adding a form(one drop down field) on checkout or before checkout, where the user can select their membership plan(role) to set that, then using this filter I found that would stop Woo Subscriptions from setting the role:

add_filter( 'woocommerce_subscriptions_update_users_role', '__return_false', 100 );

Looks like I can create a form for the user to select their role right?

https://toolset.com/forums/topic/choose-user-role-on-registration/
https://toolset.com/forums/topic/allow-users-to-select-a-role-when-registeringediting-profile-with-cred-beta-3/

I guess the issue would be how to have that form/field get integrated into the WooCommerce Cart or Checkout then have it submitted upon checkout.

I also found a plugin that might sort this out for me as well:
hidden link

Tim

#1124647

Hi Timothy,

Thanks for writing back.

If you'd prefer the custom code to change the user role(s), based on the purchased product, you can make use of "woocommerce_order_status_completed" action. This way you won't have to include a new field for the user role, during the checkout process.

Here is an article with good usage example, pretty similar to your requirement:
hidden link

Reading through the description of the "YITH Automatic Role Changer for WooCommerce" plugin, it also seems like a good fit for this task.

You can try out both these options and pick the one based on your personal preference.

Please let me know if you need any further assistance.

regards,
Waqar

#1124749

Thanks. This seems to be a lot easier than I thought. So for my case with 3 different subscription types (Silver, Gold, Platinum) would the way forward be to add that code you referenced(pasted below) 3 times and just change "$products_to_check = array( '1', '2', '3' );" to have the correct number for each subscription product and then "$user->add_role( 'new-role' );" to have the correct role for each of those products?

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( '1', '2', '3' );

    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( 'new-role' );

            // Exit the loop
            break;
    	}
    }
}
view raw

Tim

#1125113

Hi Timothy,

You won't need 3 different instances of a function and you can assign different user roles for different products, through a single function:


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();

	// update product IDs for subscription products
	$product_id_silver = 1;
	$product_id_gold = 2;
	$product_id_platinum = 3;

	$products_to_check = array( $product_id_silver, $product_id_gold, $product_id_platinum );

	foreach ( $items as $item ) {
		// check if order is for any of the subscription products
		if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
			$user = new WP_User( $order->user_id );

			// if the order is for silver product	
			if ( $item['product_id'] == $product_id_silver ) {
				// Change role to silver user role
				$user->remove_role( 'customer' );
				$user->add_role( 'silver-role' );
			}

			// if the order is for gold product	
			if ( $item['product_id'] == $product_id_gold ) {
				// Change role to gold user role
				$user->remove_role( 'customer' );
				$user->add_role( 'gold-role' );
			}

			// if the order is for platinum product	
			if ( $item['product_id'] == $product_id_platinum ) {
				// Change role to platinum user role
				$user->remove_role( 'customer' );
				$user->add_role( 'platinum-role' );
			}

			// Exit the loop
			break;
		}
	}
}

Note: you'll replace the actual product IDs (1, 2, 3) and the user role slugs used on your website.

For even more ideas and suggestions around WooCommerce hooks/filters and customization, I'll recommend consulting WooCommerce support forum and/or our certified contractors:

https://wordpress.org/support/plugin/woocommerce
hidden link

I hope this helps! Please let me know if there's anything else I can help you with.

regards,
Waqar

#1127314

Thanks, modifying this code was exactly what I needed to set memberships on subscription purchase. If I have any more specific issues regarding the membership levels and the features I want to offer based on levels I will open another ticket.

Tim