PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

assign user capabilities based on products purchased

  1. I am designing a form for people to sign up for classes. Each class is a single product. I want to assign a custom capability to each product and then add the capability to the user who purchases each product. This capability allows access to pages and/or content related to that class/product. I just need some help assigning the capabilities to the user on submission.

    Here is what I have so far:

    add_action("gform_after_submission_3", "assign__user_cap", 10, 2);
    function assign_user_cap($entry){
    	$user_id = get_current_user_id( );
    	$user = new WP_User($user_id);
    	//need to get array of field ids for the products purchased
    	//need to associate a capability with each product; perhaps as the admin name
    	foreach($field_ids as $field_id){
    	$cap_name = "access_" . $form["fields"][$field_id]["adminLabel"]; //creates custom capability "access_adminLabel" for each product
    	$user->add_cap($cap_name); //adds capability to the user who bought the product
    	return $user;

    Can you help me with my syntax and the missing functions to get the field ids for purchased products? Am I heading in the right direction here?

    Posted 11 years ago on Friday April 19, 2013 | Permalink
  2. Okay,
    I have made some progress on this but still having some problems. Here is my code:

    add_action("gform_after_submission_3", "assign_user_cap", 10, 2);
    function assign_user_cap($entry, $form){
    	$user_id = get_current_user_id( );
    	$user = new WP_User($user_id);
    	foreach(array(16,20,17,29,32,35,38,47,51,54,62) as $field_id){
    	if($entry[$field_id] >= 1){
    	$cap_name = $form["fields"][$field_id]["adminLabel"]; //creates custom capability "adminLabel" for each product
    	$user->add_cap($cap_name); //adds capability to the user who bought the product
    		}}
    	return $user;
    	}

    It is adding capabilities but just not the correct ones. It is adding some that it should and some that it should not. It is also not adding some it should. The code above is only supposed to add capabilities corresponding to classes they signed up for. Obviously the conditional I am using is not adequate somehow. Any help would be greatly appreciated.

    Posted 11 years ago on Monday April 22, 2013 | Permalink
  3. Figured it out.
    This may not be the most elegant way, but it works:
    The code:

    add_action("gform_after_submission_3", "assign_user_cap", 10, 2);
    function assign_user_cap($entry, $form){
    	$user_id = get_current_user_id( );
    	$user = new WP_User($user_id);
    	foreach(array(67,68,69,70,71,72,73,74,75,76,77) as $field_id){
    	$cap_name = $entry[$field_id]; //creates custom capability "adminLabel" for each product
    	$user->add_cap($cap_name); //adds capability to the user who bought the product
    		}
    	return $user;
    	}

    I set up an admin-only single-line text field for each product with default values set to whatever I want the capability to be for that product. Set conditional logic such that each field shows whenever the quantity for that product is not empty. The array above in the code are the field ids of those text fields. Hope that helps someone.

    Posted 11 years ago on Monday April 22, 2013 | Permalink