• Resolved mademoizellenur

    (@mademoizellenur)


    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @mademoizellenur ,

    This indeed can be confusing.

    I have taken your example code and modified it a bit. This is what is working for me to hide add to cart button for a specific user role and specific category –

    add_filter('woocommerce_is_purchasable', 'purshasabale_course', 10, 2 );
    
    function purshasabale_course( $is_purchasable, $product ) {
    
        $user = wp_get_current_user();
    	$is_purchasable = true;
    	$allowed_roles = array('administrator'); //add your user roles here
    		
    	//add your desired terms in the condition
    	if( has_term( 'accessories', 'product_cat', $product->get_id() ) ) {
    		
    		if ( array_intersect($allowed_roles, (array) $user->roles) ) {
    		    $is_purchasable = false;
    		}
    		
    	}	
    	
    	return $is_purchasable;
    
    }

    You can replace it with your desired categories and user roles. I believe those details can be inherited from the course plugin you are using.

    Thank you ??

    Thread Starter mademoizellenur

    (@mademoizellenur)

    Hey,

    Thank you for your reply don’t know why I haven’t seen your answer earlier I apologize.

    I’ve tried your code but it’s still not working for some reason. i’ve tried disabling all plugins except woocommerce and tutor lms but still nothing which is weird since it works fine for you.

    Anything else I might try? ??

    Thank you

    Hello @mademoizellenur ,

    I understand the struggle.

    As the above code worked for me while I tested only with WooCommerce, I believe you need something else from Tutor LMS.

    Please get in touch with them or take help from a Woo expert who can log in and work on your files to figure out the exact modification on the code.

    I wish I could share a direct solution but it seems out of the scope of this forum.

    Thank you ??

    Hi there,

    We’ve not heard back from you in a while, so I’m marking this thread as resolved.

    Hopefully, you were able to find a solution to your problem! If you have further questions, please feel free to open a new topic.

    Thank you ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Disable add to cart button based on different user roles for each course’ is closed to new replies.