Home Forums WC Vendors Pro Support Multiple Roles (ideally with Subscription)

NOTICE: We've Moved to a Ticket System for Support

As of August 31, 2017 (12am EST) our support forums will be retired (read-only), and we will be moving to a support ticket system.  This will allow us to better organize and answer support requests, and provide a more personalized experience as we assist our customers.

For the time being, we will leave our forums open for reading and learning while we work on creating a more robust Knowledge Base for everyone to use.

If you are a WC Vendors Pro customer please open a support ticket here. 

If you are a WC Vendors user please open a support ticket on the Wordpress.org forums.

The information on this forum is outdated and in most instances no longer relevant. Please be sure to check our documentation for the most up to date information.

https://docs.wcvendors.com/

Thank you to all of our customers!

 

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #12327
    jarvo
    Participant

    Hi
    Can I check, does the Pro version of WC Vendors allow for multiple roles?

    I need to setup multiple vendors but depending which vendor type you have, depends on what you can/can’t do.

    I’m hoping to tie this in with the WooCommerce Groups & Subscriptions plugins you see

    Thanks for your time

    #12366
    WC Vendors Support
    Participant

    When we check for the role of the logged in user, we check the array of roles, so yes, vendors can have multiple user roles.

    Cheers

    Ben

    #12371
    jarvo
    Participant

    Thanks Ben

    So to confirm; If I create a new user role called Premium Vendor, use a plugin to copy the same properties across and assign that role Premium Vendor to a user, they can have all the functionality of Vendor but with whatever else I need it to do?

    #12386
    WC Vendors Support
    Participant

    Hi Jarvo,

    No, that is incorrect. Vendors can have multiple roles, such as “Vendor” and “Customer” or “Whatever”. But if you add a “Premium Vendor” role that will not work for vendors being able to access the Vendor Dashboard at all. Only the “Vendor” role will work for that.

    This is something we’d like to write into Pro one of these days soon, it’s just on a long list of many other features we are developing for it. In time, yes, just not quite yet.

    If you’re killer with PHP and wanted to fork WC Vendors on GitHub and then make the change so it allows multiple roles you are most certainly welcome to do so, and send it back to us as a pull request. If this paragraph doesn’t make sense, don’t worry, just pretend I didn’t write it. 🙂

    Cheers

    Ben

    #15206
    jarvo
    Participant

    Hi Ben,

    I wanted to follow up on this as its been a while.

    Ok, so for me I have the following installed:
    – WC Vendors
    – WooCommerce
    – WooCommerce Subscriptions
    – WooCommerce Groups
    – Groups
    – User Role Editor
    – Advanced Custom Fields

    What I need to achieve:
    The ability to run WC Vendors but also allow for subscriptions. Depending on the subscription type, depends what Custom Field(s) get displayed

    The issue
    ACF can assign custom fields to user roles, which is exactly what I need. However, WC Vendors needs the Vendor role and WooCommerce Subscription can apply other roles (usually defaults to Subscriber on active subscription and back to Customer if subscription expires). Therefore, I can’t go this method.

    Possible solution
    Can I state, I’m by no means a coder!!! So the below may not be acceptable but it seems to work (he says!!).

    Ok, so I set up WC Vendors as I need it
    I setup WooCommerce as I need it
    I then setup WooCommerce Subscriptions but set both the active and inactive role to Vendor
    I then setup ACF Fields and assigned them to the role Subscriber (yep, not Vendor otherwise it will always show)
    Now, what I’ve done is setup Groups with a capability of basic (or whatever you need it to be) and added this to a group called basic
    Created a subscription product which once purchased, adds the user to the user group basic

    At the moment, if someone purchases a subscription, nothing will happen. So I’ve then created the following code which I’ve added to my themes function file:

    
    ###################################################
    # Assign Second Role Depending on Subscription Group
    ###################################################
    
    #Checking for Capabilities EXCLUDE administrator
    if (current_user_can('administrator') || is_admin() ) {
    
    } elseif ( current_user_can( 'basic' )) {	
    
    #if ( current_user_can( 'basic' ) ) {
    
    	#Test whether the current user belongs to a group
    	#echo '<h1>The current user has basic capabilities</h1>'; #debug
    		
    	#global $wpdb;
    	#Setup all the required variables
    	$user_ID = get_current_user_id();
    	$metakey = "capabilities";
    	$set_default_role = 'a:1:{s:6:"vendor";b:1;}';
    	$add_second_role = 'a:2:{s:6:"vendor";b:1;s:10:"subscriber";b:1;}';
    	
    	#Create a function to retrieve the umeta ID for the correct meta key/value based on a user ID
    	function get_umeta_id_by_key( $user_ID, $meta_key ) {
    		global $wpdb;
    		$umeta_id = $wpdb->get_var( $wpdb->prepare("SELECT umeta_id FROM $wpdb->usermeta WHERE user_ID = %d AND meta_key = %s", $user_ID, $meta_key) );
    		if( $umeta_id != '' )
    		return (int) $umeta_id;
    	
    		return false;
    	}
    	
    	#Assign the result as a variable to pass to our SQL
    	$meta_id = get_umeta_id_by_key( $user_ID, $metakey );
    	
    	#Setup the UPDATE sql statement based on our variables
    	$sql = $wpdb->prepare( " UPDATE $wpdb->usermeta SET meta_value = '$add_second_role' WHERE $wpdb->usermeta.umeta_id = %d ", $meta_id);
    	
    	function add_subscriber_role_queries( $sql ) {
    		#Run the query
    		global $wpdb;
    		$wpdb->query($sql);	
    		$wpdb->print_error();
    		$wpdb->flush();
    		/*
    		echo '<code>';
    			print_r( $sql ); # <code>print_r</code> the array data inside the 1st argument for testing/debugging
    		echo '</code>';
    		*/
    	}
    	#Add_action( $tag, $function_to_add, $priority, $accepted_args );
    	add_action( 'add_subscriber_role', 'add_subscriber_role_queries', 10, 2 );
    	
    	#Use do_action as needs to run without any means of trigger (eg. post saved)
    	do_action( 'add_subscriber_role', $sql);
    	
    } else {
    	#Test whether the current user belongs to a group
    	#echo '<h1>The current user doesn\'t have basic capabilities</h1>'; #debug
    	#global $wpdb;
    	#Setup all the required variables
    	$user_ID = get_current_user_id();
    	$metakey = "capabilities";
    	$set_default_role = 'a:1:{s:6:"vendor";b:1;}';
    	$add_second_role = 'a:2:{s:6:"vendor";b:1;s:10:"subscriber";b:1;}';
    	
    	#Create a function to retrieve the umeta ID for the correct meta key/value based on a user ID
    	function get_umeta_id_by_key( $user_ID, $meta_key ) {
    		global $wpdb;
    		$umeta_id = $wpdb->get_var( $wpdb->prepare("SELECT umeta_id FROM $wpdb->usermeta WHERE user_ID = %d AND meta_key = %s", $user_ID, $meta_key) );
    		if( $umeta_id != '' )
    		return (int) $umeta_id;
    	
    		return false;
    	}
    	
    	#Assign the result as a variable to pass to our SQL
    	$meta_id = get_umeta_id_by_key( $user_ID, $metakey );
    	
    	#Setup the UPDATE sql statement based on our variables
    	$sql = $wpdb->prepare( " UPDATE $wpdb->usermeta SET meta_value = '$set_default_role' WHERE $wpdb->usermeta.umeta_id = %d ", $meta_id);
    	
    	#Create the function to process the query
    	function remove_subscriber_role_queries( $sql ) {
    		#Run the query
    		global $wpdb;
    		$wpdb->query($sql);
    		$wpdb->print_error();
    		$wpdb->flush();
    	}
    	#Add_action( $tag, $function_to_add, $priority, $accepted_args );
    	add_action( 'remove_subscriber_role', 'remove_subscriber_role_queries', 10, 2 );
    	
    	#Use do_action as needs to run without any means of trigger (eg. post saved)
    	do_action( 'remove_subscriber_role', $sql);	
    }
    

    Basically, this is what ‘should’ happen with the code:
    1) It checks to see if the user belongs to a specific group (setup via Groups), in this case the ‘basic’ group
    2) If the user does, it gets the user meta ID from the current logged in user ID and runs a simple UPDATE SQL statement to alter the users capability meta by adding the subscriber role
    3) Once the user has the additional role, the ACF fields then kick in

    If the users subscription expires, they’re removed from the group ‘basic’ (default WooCommerce subscription behaviour).
    The function in the theme file will always check the user and whether they belong to a group. If they don’t the second SQL statement runs, UPDATING the user again but defaulting back to the vendor role

    I hope the code is ok but I’m sure it can be tidied/improved. The URE plugin remains active but I don’t use it apart from being able to easily see on the USERS page (as an admin) whether users have the multiple roles

    I’d really appreciate your thoughts on this though

    Many thanks

    #15211
    jarvo
    Participant

    Minor amend, please change:

    
    $metakey = "capabilities";
    

    To:

    
    $metakey = "$wpdb->capabilities";
    
    #15222
    WC Vendors Support
    Participant

    Looks like you’ve had some fun with this one! 🙂 Well done! If this solution works for you — keep it! I never try to fix something that isn’t broken. 🙂

    #15336
    jarvo
    Participant

    Thanks Ben

    Am going to be a total nightmare now and say… IGNORE THE ABOVE!!

    The whole thing can be achieved by simply doing the following:
    1) Install WC Vendors, WooCommerce, WooCommerce Subscriptions, WooCommerce Groups, Groups, User Role Editor and finally Advanced Custom Fields (ACF)
    2) Activate all plugins
    3) Set the overall default role to Vendor
    4) In WooCommerce Subscriptions, I set both active and expired subscriptions to Vendor (otherwise you can’t use WC Vendors)
    5) I setup a new capability in Groups called basic and assigned it to a new group called basic (this is in line with my subscription type)
    6) Create a new subscription product (set the length of subscription as required), ensure you add your new group (basic in my case)
    7) I wanted particular custom fields to be applied to a specific user type, so setup your ACFs and set the location rule to USER and SUBSCRIBER
    8) Add the following code to your functions file:

    
    ###################################################
    # Assign Second Role Depending on Subscription Group
    ###################################################
    # Change basic to whatever you setup in your groups
    if ( current_user_can( 'basic' )) {
    	function add_secondary_role( $user_id ) {
    		$user_id = get_current_user_id();
    		$user = get_user_by('id', $user_id);
    		$user->add_role('subscriber');
    	}
    	add_action( 'add_subscriber_role', 'add_secondary_role', 10, 2 );
    	do_action( 'add_subscriber_role', $user_id);
    }
    

    9) Add the following code to your WooCommerce My Account template:

    
    #Ensure this is set to the user rule you specified in your ACF setup
    if( current_user_can('subscriber') ):
    	acf_form(array(
    		'post_id' => 'user_'.get_current_user_id(),
    		'submit_value'	=> 'Update Details' 
    	)); 
    
    endif;
    

    Now, when someone subscribes, the code in the functions file will check whether they have a specific capability (basic), if they do, add the second role of subscriber. As the user then has a role of subscriber, they will be able to access the ACF fields on their My Account page.

    Once the subscription expires, they will automatically be removed from the group and therefore the second role is removed thus disabling the custom fields unless they renew! The great thing is, if they renew, they won’t lose the data in the custom fields!

    I really hope that helps someone out as it’s taken ages to sort what is actually a simple solution :-/

    Cheers

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘WC Vendors Pro Support’ is closed to new topics and replies.
This website uses cookies to ensure you get the best experience on our website.