Disable add to cart button based on different user roles for each course
-
Hello,
I’m not sure if this is the right place to post this, forgive me if it’s not.
I’ve been using woocommerce with tutor lms and ultimate member plugins and I’ve been trying to restrict adding courses to cart based on user roles for each course and I still haven’t figured how to do this.
Basically, I need students to be able to add only the courses that are limited to their level (user role) to the cart.
The course access restriction works fine with the ultimate member plugin, they can’t access a course if they don’t have the required level but they still can add it to the cart and proceed to checkout, even though they wouldn’t be able to take the course afterwards.
I don’t want to completely hide the courses on my pages, some students would be left with an almost empty website, I want them to be able to see the description of the page where the course shortcode is but not be able to buy it or access it if they don’t have the required level.
I have 6 student levels (ultimate members user roles), and for each course I want to be able to give specific user roles only the permission to purchase the course.
I’ve tried this solution for a “Conversation classes” course, it worked but only for one course and one user role (result photo)
add_filter('woocommerce_is_purchasable', 'purchasable_course', 10, 2 ); function purchasable_course( $is_purchasable, $product ) { $categories = array('conversation-classes'); $allowed_user_roles = array('um_elementary'); $user = wp_get_current_user(); if( array_intersect( $allowed_user_roles, $user->roles ) ) { if( has_term( $categories, 'product_cat', $product->get_id() ) ) { return false; } return $is_purchasable; } }
So I tried another one, and it seems logical in my head but can’t seem to be able to make it work either.
I check if the course category is ‘conversation-classes’ then I check if the user role is in the array of the allowed user roles and set the $is_purchase variable to true, and to false if not. This disables the add to cart function for all courses. I’m not sure for other user roles.
add_filter('woocommerce_is_purchasable', true, 10, 2 ); function purshasabale_course( $is_purchasable, $product ) { $user = wp_get_current_user(); $is_purchasable = false; if( has_term( 'conversation-classes', 'product_cat', $product->get_id() ) ) { if( in_array ( $user->roles, array('um_intermediate','um_pre-intermediate') ) ) { $is_purchasable = true; } } return $is_purchasable; }
Is there any simple way I could do this? I would be really thankful if anyone could help me with this I’ve been trying different solutions for days.
Thank you in advance.
- The topic ‘Disable add to cart button based on different user roles for each course’ is closed to new replies.