Home › Toolset Professional Support › [Resolved] Woocommerce subscription roles
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 |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10: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/Kolkata (GMT+05:30)
This topic contains 27 replies, has 3 voices.
Last updated by Minesh 1 month ago.
Assisted by: Minesh.
First of all, as you are using the optimization plugin:
=> https://toolset.com/faq/how-to-use-optimization-plugins-with-toolset/
In order to make sure everything should work smoothly and there should be no conglict, I've added the following links to exclude those CSS and JS file:
JS files excluded:
=> hidden link
wp-content/plugins/types/vendor/toolset/common-es/public/toolset-common-es-frontend.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/js/conditional.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/js/main.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/js/date.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/js/validation.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/jquery.validate.js wp-content/plugins/types/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/additional-methods.min.js wp-content/plugins/types/vendor/toolset/toolset-common/res/lib/bootstrap4/js/bootstrap.bundle.min.js wp-content/plugins/types/vendor/toolset/toolset-common/res/lib/select2/select2.js wp-content/plugins/types/vendor/toolset/toolset-common/res/lib/toolset-event-manager/toolset-event-manager.min.js wp-content/plugins/types/vendor/toolset/toolset-common/res/js/toolset-select2-compatibility.js wp-content/plugins/types/vendor/toolset/toolset-common/res/js/toolset-parser.js wp-content/plugins/types/vendor/toolset/toolset-common/utility/js/utils.js wp-content/plugins/types/vendor/toolset/toolset-common/visual-editor/res/js/codemirror/lib/codemirror.js wp-content/plugins/types/vendor/toolset/toolset-common/visual-editor/res/js/icl_editor_addon_plugin.js wp-content/plugins/toolset-maps/vendor/toolset/common-es/public/toolset-common-es-frontend.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/js/conditional.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/js/main.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/js/date.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/js/validation.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/jquery.validate.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/additional-methods.min.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/res/lib/bootstrap4/js/bootstrap.bundle.min.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/res/lib/select2/select2.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/res/lib/toolset-event-manager/toolset-event-manager.min.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/res/js/toolset-select2-compatibility.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/res/js/toolset-parser.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/utility/js/utils.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/visual-editor/res/js/codemirror/lib/codemirror.js wp-content/plugins/toolset-maps/vendor/toolset/toolset-common/visual-editor/res/js/icl_editor_addon_plugin.js wp-content/plugins/toolset-maps/resources/js/wpv_addon_maps.js wp-content/plugins/cred-frontend-editor/vendor/toolset/common-es/public/toolset-common-es-frontend.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/js/conditional.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/js/main.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/js/date.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/js/validation.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/jquery.validate.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/additional-methods.min.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/res/lib/bootstrap4/js/bootstrap.bundle.min.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/res/lib/select2/select2.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/res/lib/toolset-event-manager/toolset-event-manager.min.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/res/js/toolset-select2-compatibility.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/res/js/toolset-parser.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/utility/js/utils.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/visual-editor/res/js/codemirror/lib/codemirror.js wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/visual-editor/res/js/icl_editor_addon_plugin.js wp-content/plugins/cred-frontend-editor/public/js/frontend.js wp-content/plugins/cred-frontend-editor/public/js/media_manager_basic.js wp-content/plugins/cred-frontend-editor/public/js/select2_frontend.js wp-content/plugins/toolset-blocks/vendor/toolset/common-es/public/toolset-common-es-frontend.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/js/conditional.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/js/main.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/js/date.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/js/validation.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/jquery.validate.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/additional-methods.min.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/res/lib/bootstrap4/js/bootstrap.bundle.min.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/res/lib/select2/select2.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/res/lib/toolset-event-manager/toolset-event-manager.min.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/res/js/toolset-select2-compatibility.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/res/js/toolset-parser.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/utility/js/utils.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/visual-editor/res/js/codemirror/lib/codemirror.js wp-content/plugins/toolset-blocks/vendor/toolset/toolset-common/visual-editor/res/js/icl_editor_addon_plugin.js wp-content/plugins/toolset-blocks/public/js/views-frontend.js wp-content/plugins/toolset-blocks/vendor/toolset/blocks/public/js/frontend.js wp-content/plugins/toolset-blocks/vendor/toolset/blocks/public/vendor/glide/glide.min.js wp-content/plugins/toolset-blocks/vendor/toolset/blocks/public/vendor/lightbox/js/lightbox.min.js wp-content/plugins/types-access/vendor/toolset/common-es/public/toolset-common-es-frontend.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/js/repetitive.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/js/conditional.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/js/main.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/js/date.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/js/validation.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/jquery.validate.js wp-content/plugins/types-access/vendor/toolset/toolset-common/toolset-forms/lib/js/jquery-form-validation/additional-methods.min.js wp-content/plugins/types-access/vendor/toolset/toolset-common/res/lib/bootstrap4/js/bootstrap.bundle.min.js wp-content/plugins/types-access/vendor/toolset/toolset-common/res/lib/select2/select2.js wp-content/plugins/types-access/vendor/toolset/toolset-common/res/lib/toolset-event-manager/toolset-event-manager.min.js wp-content/plugins/types-access/vendor/toolset/toolset-common/res/js/toolset-select2-compatibility.js wp-content/plugins/types-access/vendor/toolset/toolset-common/res/js/toolset-parser.js wp-content/plugins/types-access/vendor/toolset/toolset-common/utility/js/utils.js wp-content/plugins/types-access/vendor/toolset/toolset-common/visual-editor/res/js/codemirror/lib/codemirror.js wp-content/plugins/types-access/vendor/toolset/toolset-common/visual-editor/res/js/icl_editor_addon_plugin.js wp-content/plugins/woocommerce-views/library/toolset/woocommerce-views/res/js/wcviews-frontend.js
CSS files excluded:
=> hidden link
wp-content/plugins/toolset-blocks/public/css/views-frontend.css wp-content/plugins/cred-frontend-editor/vendor/toolset/toolset-common/res/lib/select2/select2.css wp-content/plugins/toolset-maps/resources/css/toolset_maps_fixes.css
Now, regarding the issue, when I try to register the user,
=> hidden link
As per the following Docs:
- https://toolset.com/course-lesson/charge-users-for-registering-premium-accounts/
But when I try to add username and email it redirect me on the following page:
- hidden link
But on the above page it asks usernamd and password again when I set the configuration as per the WC subscription settings as per the Step 5:
- https://toolset.com/course-lesson/using-the-woocommerce-subscriptions-plugin-with-toolset/#how-to-setup-woocommerce-subscription
Here is a sandbox site and you can auto-login to it using the following link:
- hidden link
Can you please try to setup the above sandbox site and check if you see the same with above sandbox site. You can add all required plugins and only 1-2 products in order to check this further.
Thanks!
You wrote:
But on the above page it asks usernamd and password again when I set the configuration as per the WC subscription settings as per the Step 5:
- https://toolset.com/course-lesson/using-the-woocommerce-subscriptions-plugin-with-toolset/#how-to-setup-woocommerce-subscription
When i look at the user form of art pakket 2 when enabling what you wrote above, the form gives this error message:
You selected to 'Enable registration on the Checkout' in WooCommerce.
This means that WooCommerce will automatically create users when paying.
You are also using Toolset Forms to create new users, so for each registration, your site will have two users.
To avoid this, go to the WooCommerce settings and disable 'Enable registration on the Checkout'.
So that means it is not possible to create different users with different roles when i disable that function , since WC subscriptions let you only register users with ONE role type. Am i right? So that means i can not use WC subscriptions when i have different subscriptions when users should get different roles as well.
Please let me know what you think, because rebuilding the whole process in a sandbox will be so much work. Besides that i can't use wc subscriptions on that because of licence limitations.
Oh and i think that the option that i selected here (See printscreen) is the same as said in the toolset documentation: Allow customers to create an account on checkout.
The option i checked is: allow subscription clients t create an account on checkout.
- when i tested that yesterday i was not asked to re-enter the username and password on this page: - hidden link ? Can that have something to do with the function you made with the repopulating of the email address? (Which i really like by the way)
I do not do anything - anything that shows or hides is based on the selection of those options that you mentioned.
Ok - so to update the role after successful subscription purchase:
Here is the reference ticket that you should check:
- https://toolset.com/forums/topic/how-do-you-integrate-woocommerce-susbscription-to-toolset-access/#post-1125113
As you are using WC subscription - if you want to delete the posts when subscription role changes to some other role:
=> https://toolset.com/forums/topic/delete-users-posts-when-woocommerce-subscription-cancels/#post-608852
Change the user role:
=> https://toolset.com/forums/topic/woocommerce-subscriptions-and-toolset-access-multiple-role-issue/#post-2050665
So with this code I should let the users directly buy the product, and not first let them fill in a toolset user form? I guess that was the confusing part for me...
I'm testing now without the toolset user form. When I directly order a subscription product: hidden link
1. I can fill in the woocommerce order page, but I get a 404 after submitting it, when I disable toolset plugins I get the normal confirmation page (see print screen) a customer gets created though.
2. So a customer gets created with the role "Klant" . But I need the customer to get the role "Pakket 1" for this specific product. So I used the code from the link above and edited the first one for my situation. It doesn't work yet. Perhaps you can see what I'm doing wrong. If you can help me to set the art_pakket one correct, I think I can manage the other ones. (gold and platinum are examples)
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_art_pakket = 2628;
$product_id_gold = 2;
$product_id_platinum = 3;
$products_to_check = array( $product_id_silver, $product_id_gold, $product_id_art_pakket );
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 Art pakket
if ( $item['product_id'] == $product_id_art_pakket ) {
// Change role to silver user role
$user->remove_role( 'Klant' );
$user->add_role( 'Pakket 1' );
}
// 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;
}
}
}
I understand that I'm becoming a pain in the *** with all these questions. I'm sorry.
We've got it almost working, it's the last things before we can go live.
I found why I get a 404 after ordering, the subscriptions plugin only gave the role 'klant' (customer) and with toolset access activated, that role is not allowed to see the order completed page.
Now I've set subscriptions to give every client the role of 'Pakket 1' which is the free subscription. (by the way woocommerce subscriptions also gives the role 'Klant' which is neccesary I guess to make the payment processing possible)
I have altered the role switching code into this, but it still doesn't work:
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_art_pakket = 2628;
$product_id_gold = 2;
$product_id_platinum = 3;
$products_to_check = array( $product_id_silver, $product_id_gold, $product_id_art_pakket );
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 Art pakket
if ( $item['product_id'] == $product_id_art_pakket ) {
// Change role to Pakket 2 user role
$user->remove_role( 'Pakket 1' );
$user->add_role( 'Pakket 2' );
}
// 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;
}
}
}
Sorry I made a mistake in the above code.
So every new subscriber gets role 'Pakket 1' which is the free package. But depending on which product is bought, that must stay the same when they buy Pakket 1, (art Pakket) and change to Pakket 2, 3 or 4 when buying the payed subscriptions.
I altered the code your colleague posted for the art Promotion subscription (role: 'Pakket 2') and left the others untouched. It's still not working, but perhaps it's just a typo which you can solve easily. I'm no programmer unfortunately... :
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_art_promotion_pakket = 6086;
$product_id_gold = 2;
$product_id_platinum = 3;
$products_to_check = array( $product_id_silver, $product_id_gold, $product_id_art_promotion_pakket );
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 Art promotion akket
if ( $item['product_id'] == $product_id_art_promotion_pakket ) {
// Change role to Pakket 2 user role
$user->remove_role( 'Pakket 1' );
$user->add_role( 'Pakket 2' );
}
// 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;
}
}
}
As I shared before:
- https://toolset.com/forums/topic/woocommerce-subscriptions-and-toolset-access-multiple-role-issue/#post-2050665
- https://toolset.com/forums/topic/how-do-you-integrate-woocommerce-susbscription-to-toolset-access/#post-1125113
You will have to pass the Role slug. As you can see with above tickets, the roles are added or removed as given under:
$user->remove_role( 'customer' ); $user->add_role( 'silver-role' );
So, what if you try to change the if condition if your code as given under:
// if the order is for Art promotion akket if ( $item['product_id'] == $product_id_art_promotion_pakket ) { // Change role to Pakket 2 user role $user->remove_role( 'pakket_1' ); $user->add_role('pakket_2'); }
I hope above solution should help you to fix the issue.
Okay, so i missed the underscores in Pakket_1 and 2. I changed that and it's still not working. As far as i know i'm doing exactly as you say and is mentioned in the 2 forum topics you mention.
Can you please login my website and see what's going wrong?
You can test this for example with this product: hidden link After buying with the snippet off, you'll get 2 roles: klant and Pakket 1. With the snippet on Pakket 1 should change into Pakket 2
(You can buy actually any subscription on this page, except the Art Pakket, because that is correct to have the Pakket 1 role: hidden link)
i added the snippet here: hidden link
And i altered it as you mentioned, and added all the existing 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();
// Define product IDs for subscription products
$product_id_Art_Promotion_Pakket = 6086;
$product_id_Art_Exclusive_Pakket = 6088;
$product_id_Art_Easy_Pakket = 6089;
$product_id_Art_Gallery_Display_Pakket = 6090;
$products_to_check = array( $product_id_Art_Promotion_Pakket, $product_id_Art_Exclusive_Pakket, $product_id_Art_Easy_Pakket, $product_id_Art_Gallery_Display_Pakket );
foreach ( $items as $item ) {
// Check if order is for any of the subscription products
if ( $order->get_user_id() > 0 && in_array( $item->get_product_id(), $products_to_check ) ) {
$user = new WP_User( $order->get_user_id() );
// If the order is for Art Promotion Pakket
if ( $item->get_product_id() == $product_id_Art_Promotion_Pakket ) {
// Change role to 'Pakket_2' user role
$user->remove_role( 'Pakket_1' );
$user->add_role( 'Pakket_2' );
}
// If the order is for Art Exclusive Pakket
if ( $item->get_product_id() == $product_id_Art_Exclusive_Pakket ) {
// Change role to 'Pakket_3' user role
$user->remove_role( 'Pakket_1' );
$user->add_role( 'Pakket_3' );
}
// If the order is for Art Easy Pakket
if ( $item->get_product_id() == $product_id_Art_Easy_Pakket ) {
// Change role to 'Pakket_4' user role
$user->remove_role( 'Pakket_1' );
$user->add_role( 'Pakket_4' );
}
// If the order is for Art Gallery Display Pakket
if ( $item->get_product_id() == $product_id_Art_Gallery_Display_Pakket ) {
// Change role to 'Pakket_5' user role
$user->remove_role( 'Pakket_1' );
$user->add_role( 'Pakket_5' );
}
// Exit the loop after processing the first matching product
break;
}
}
}
I noticed that when you place the order the user is not attached to the order.
So, to attach the user to order, I've adjusted the code as given under with your code snippet:
=> hidden link
/// disable WooCommerce Subscription action so that it should allow us to add/remove custom role using "woocommerce_order_status_completed" add_filter( 'woocommerce_subscriptions_update_users_role', '__return_false', 100 ); add_action( 'woocommerce_order_status_completed', 'func_change_role_on_purchase',10,1); function func_change_role_on_purchase( $order_id ) { /////// setting the user for the order $order = wc_get_order( $order_id ); $billing_email = $order->get_billing_email(); $order_user = get_user_by( 'email', $billing_email ); if($order_user){ //$order->set_customer_id( $order_user->ID ); update_post_meta($order_id, '_customer_user', $order_user->ID); } ///// getting the order details $order = wc_get_order( $order_id ); $items = $order->get_items(); // Define product IDs for subscription products $product_id_Art_Promotion_Pakket = 6086; $product_id_Art_Exclusive_Pakket = 6088; $product_id_Art_Easy_Pakket = 6089; $product_id_Art_Gallery_Display_Pakket = 6090; $products_to_check = array( $product_id_Art_Promotion_Pakket, $product_id_Art_Exclusive_Pakket, $product_id_Art_Easy_Pakket, $product_id_Art_Gallery_Display_Pakket ); foreach ( $items as $item ) { // Check if order is for any of the subscription products if ( $order->get_user_id() > 0 && in_array( $item->get_product_id(), $products_to_check ) ) { $user = new WP_User($order_user->ID); // If the order is for Art Promotion Pakket if ( $item->get_product_id() == $product_id_Art_Promotion_Pakket ) { // Change role to 'Pakket_2' user role $user->remove_role('pakket_1'); $user->add_role('pakket_2'); //$result = wp_update_user(array('ID'=>$user_id, 'role'=>$new_role)); } // If the order is for Art Exclusive Pakket if ( $item->get_product_id() == $product_id_Art_Exclusive_Pakket ) { // Change role to 'Pakket_3' user role $user->remove_role( 'pakket_1' ); $user->add_role( 'pakket_3' ); } // If the order is for Art Easy Pakket if ( $item->get_product_id() == $product_id_Art_Easy_Pakket ) { // Change role to 'Pakket_4' user role $user->remove_role( 'pakket_1' ); $user->add_role( 'pakket_4' ); } // If the order is for Art Gallery Display Pakket if ( $item->get_product_id() == $product_id_Art_Gallery_Display_Pakket ) { // Change role to 'Pakket_5' user role $user->remove_role( 'pakket_1' ); $user->add_role( 'pakket_5' ); } // Exit the loop after processing the first matching product break; } } }
- Please note that you will have to use the correct role slug (also mind the first letter as lower case for your role name, for example: pakket_1 instead of Pakket_1).
I can see the role pakket_2 is attached to new user but it was not allowing to remove the pakket_1 role.
As suspected that WC Subscription was issue that was preventing the removal of the role from user. So, I found this:
- https://woocommerce.com/document/subscriptions/develop/prevent-role-changes-by-subscriptions/
I've added the following line to your code snippet:
add_filter( 'woocommerce_subscriptions_update_users_role', '__return_false', 100 );
When I place the order using the following product:
- hidden link
I can see two roles assigned (Customer, Pakket 2).
You can also check the following links that may help you later on if required:
- https://stackoverflow.com/questions/63073288/change-user-role-based-on-woocommerce-active-subscription
- https://stackoverflow.com/questions/55230729/switch-user-role-on-subscription-expiration-in-woocommerce
You're the best!
It works, but there was only one problem, the free subscription product 'Art Pakket' didn't work anymore, it only got the 'klant' role and not the Pakket 1. I guess all the extra roles besides 'klant' (customer) are automatically deleted now?
So i added that package: Art Pakket to the code and now it's working.
```
// If the order is for Art Pakket
if ( $item->get_product_id() == $product_id_Art_Pakket ) {
// Change role to 'Pakket_1' user role
$user->remove_role('pakket_1');
$user->add_role('pakket_1');
}
```
Or is that not the right way to solve it?
It's this product: $product_id_Art_Pakket = 2628;
<hr />
```
//add_action( 'woocommerce_subscriptions_updated_users_role', 'tssupp_autoexpire_posts', 100, 3);
//add_action( 'woocommerce_subscription_status_active', 'tssupp_autoexpire_posts', 99, 3);
function tssupp_autoexpire_posts( $subscription ){
$order = wc_get_order( $subscription->get_parent_id() );
// Get an instance of the customer WP_User Object
$user = $order->get_user();
echo "<pre>";
print_r($subscription)."<br/>";
print_r($user)."<br/>";
//print_r($role_old);
exit;
}
add_filter( 'woocommerce_subscriptions_update_users_role', '__return_false', 100 );
add_action( 'woocommerce_order_status_completed', 'func_change_role_on_purchase',10,1);
function func_change_role_on_purchase( $order_id ) {
/////// setting the user for the order
$order = wc_get_order( $order_id );
$billing_email = $order->get_billing_email();
$order_user = get_user_by( 'email', $billing_email );
if($order_user){
//$order->set_customer_id( $order_user->ID );
update_post_meta($order_id, '_customer_user', $order_user->ID);
}
///// getting the order details
$order = wc_get_order( $order_id );
$items = $order->get_items();
// Define product IDs for subscription products
$product_id_Art_Promotion_Pakket = 6086;
$product_id_Art_Exclusive_Pakket = 6088;
$product_id_Art_Easy_Pakket = 6089;
$product_id_Art_Gallery_Display_Pakket = 6090;
$product_id_Art_Pakket = 2628;
$products_to_check = array( $product_id_Art_Pakket, $product_id_Art_Promotion_Pakket, $product_id_Art_Exclusive_Pakket, $product_id_Art_Easy_Pakket, $product_id_Art_Gallery_Display_Pakket );
foreach ( $items as $item ) {
// Check if order is for any of the subscription products
if ( $order->get_user_id() > 0 && in_array( $item->get_product_id(), $products_to_check ) ) {
$user = new WP_User($order_user->ID);
// If the order is for Art Promotion Pakket
if ( $item->get_product_id() == $product_id_Art_Promotion_Pakket ) {
// Change role to 'Pakket_2' user role
$user->remove_role('pakket_1');
$user->add_role('pakket_2');
//$result = wp_update_user(array('ID'=>$user_id, 'role'=>$new_role));
}
// If the order is for Art Pakket
if ( $item->get_product_id() == $product_id_Art_Pakket ) {
// Change role to 'Pakket_1' user role
$user->remove_role('pakket_1');
$user->add_role('pakket_1');
}
// If the order is for Art Exclusive Pakket
if ( $item->get_product_id() == $product_id_Art_Exclusive_Pakket ) {
// Change role to 'Pakket_3' user role
$user->remove_role( 'pakket_1' );
$user->add_role( 'pakket_3' );
}
// If the order is for Art Easy Pakket
if ( $item->get_product_id() == $product_id_Art_Easy_Pakket ) {
// Change role to 'Pakket_4' user role
$user->remove_role( 'pakket_1' );
$user->add_role( 'pakket_4' );
}
// If the order is for Art Gallery Display Pakket
if ( $item->get_product_id() == $product_id_Art_Gallery_Display_Pakket ) {
// Change role to 'Pakket_5' user role
$user->remove_role( 'pakket_1' );
$user->add_role( 'pakket_5' );
}
// Exit the loop after processing the first matching product
break;
}
}
}
```
Glad to know that solution I shared help you to resolve your issue.
Well - you can use the hook for your desired customizations to add/remove the role to user and make sure its working as expected.
However - for more deep insight you should check with WooComemrce Subscription support as they will have the best idea if you need any deep guidance.