Membership plugin - user level by user meta?

Hello, I've recently started a topic regarding how to use invite codes for access to a specific level (and not for a more basic level), but couldn't get a solution yet. Nevermind.

I almost did it, using a totally different system for registration and not using invite codes at all. I'm planning to use Gravity Forms, validating a field against a database table, in order to add a user to the correct level.

(that's why I open a new topic: only the final goals is the same, I'm discussing a totally different technique now)

I'm just stuck at the final point: to make my system work, I need to be able to select the membership level based on user meta.

What value/key should I use? I've looked into the database, but I can find anything clearly self-explanatory.

My GUESS is that the meta key is "current-membership-step" , and the correct value is numeric, i.e. the ID of the membership level as shown in the db table named "m_membership_levels".

EXAMPLE:

I have 2 levels: BASIC and PRO.
BASIC has ID=1
PRO has ID=2

To add a user to the PRO level via user meta, I'd set the user meta key like this:
"current-membership-step" = 1

------------------------------------------------

QUESTIONS:

1) is my theory correct?

2) even if it is, I'm worried about 2 other meta keys
-A- membership_permissions_updated
-B- _membership_key

I need to know whether those 2 keys need to be set manually, or are auto-generated by your plugin when a user is registered with the described system.

Or, eventually, if the plugin is unable to serve my purpose, even with my method.
In which case, please share with me the complete list of database tables, as it would prove unfit to serve my only need for this website.

If my question is not 100% clear, please let me know and I'll provide more infos.
Sorry for the loooong post and thanks a lot in advance for your help.

Regards
Max

  • Jude

    Hi again @Max

    I was going to respond to your other thread but I see this one here. A closer look shows me that if you force invite codes than ALL levels require an invite code even though we are validating for just one and bypassing this may not be as easy as I first thought.

    I was going to get the developers opinion on the best way forward.

    Here is how you get a level id.

    $m = new Membership_Model_Member($user_id) ;
    $level = $m->get_level_ids();

    Alternately you can use a query like this directly if you prefer

    SELECT level_id FROM <strong>MEMBERSHIP_TABLE_RELATIONS</strong> WHERE user_id = <strong>USER_ID</strong> AND level_id > 0

    Here MEMBERSHIP_TABLE_RELATIONS and USER ID are from your local install depending on the user and your table prefix.

    So you're final code will go something like

    if ( 'valid' == get_user_meta( $user_id, 'code') )
        $m = new Membership_Model_Member($user_id) ;
        $m->add_level ( $tolevel_id ) ;

    Here are the tables the plugin creates

    add_global_table( 'm_membership_levels' );
    		add_global_table( 'm_membership_rules' );
    		add_global_table( 'm_membership_relationships' );
    		add_global_table( 'm_member_payments' );
    		add_global_table( 'm_subscriptions' );
    		add_global_table( 'm_subscriptions_levels' );
    		add_global_table( 'm_subscription_transaction' );
    		add_global_table( 'm_subscriptionmeta' );
    		add_global_table( 'm_urlgroups' );
    		add_global_table( 'm_communications' );

    Also for your last question, you will not have problems with that if you hook late enough where the whole membership create process is complete and those keys are already set.

    Hope that helps

    Jude

  • Max M.

    Thanks for the quick reply.
    I guess I generated some level of misunderstanding. I'll try to re-explain to make sure we understand each other. :slight_smile:

    Actually, the system I found doesn't require adding code (ok... almost, but I managed that part easily).

    I'm unsure about your familiarity with Gravity Forms, suffice to say that with addons and some custom code I managed to create a subscription form with a radio button to see if the user wants to subscribe to the special level or to the normal one.

    IF "special" is selected, THEN another field appears, asking for a code;
    the inserted code is then validated against a series of previously inserted codes.

    Depending on the selected level, I can programmatically set a new user's meta key/value.
    The logic is, roughly: "IF special, THEN key=A; ELSEIF normal, THEN key=B".

    The form works, the validation works... so now the question is:

    if I just set:
    "current-membership-step" = 1
    or instead
    "current-membership-step" = 2

    (where "1" and "2" are the IDs of my levels)

    would it work?
    That simple?
    Or should I set something else?

    Thanks, regards
    Max

  • Jude

    Hi there @Max M.

    Really strange this is the second time I did not get notified when you posted a reply. Any hoo You're not annoying at all you're a valuable member.

    Ok since you got everything up to the last point figured out I can show you the best way to add the member in. Im assuming since you're able to query the user_meta table you also have a $user_id handy as part of the workflow.

    Here is the final piece to the puzzle ( Hopefully :wink: )

    add_action( 'user_register', 'add_custom_level', 10, 1 );
    
    function add_custom_level( $user_id ) {
    
    if ( ) // Your conditions
        $m = new Membership_Model_Member($user_id) ;
        $m->assign_level ( 1 ) ;
    else {
        $m = new Membership_Model_Member($user_id) ;
        $m->assign_level ( 2 ) ;
    }
    
    }

    Sorry the response took a while longer than expected. Appreciate your patience

    Jude

    • Max M.

      Hi and thanks for your constant help.
      I managed to create this code:

      <?php
      /**
       * Plugin Name: Membership level on User Meta by Max
       * Description: Inserisci chi valida il seriale nel gruppo "proprietario verificato"
       */
      
      //decide based on value for key "unit-verified-owner", step 1 - get value
      $isowner = get_user_meta ( $user_id, 'unit-verified-owner' );
      
      //changed to work on user update rather than registration
      add_action( 'edit_user_profile_update', 'add_custom_level', 10, 1 );
      
      function add_custom_level( $user_id ) {
      
      //if key value is 1, give access level which has id number 1
      if ( $isowner == 1 ) {
          $m = new Membership_Model_Member($user_id) ;
          $m->assign_level ( 1 ) ;
          }
      
      //else give access level which has id 4, the standard level for registration
      else {
          $m = new Membership_Model_Member($user_id) ;
          $m->assign_level ( 4 ) ;
      }
      
      }

      This doesn't do anything.
      I'd love if you could point out any error... or maybe the mistake is that I wrote it as a custom plugin?
      In that case, how could I modify it to work?

      If I'll make it, I'll have a great setup on the site, and I'd be interested to share it with the community.
      I guess I'm not the only one using WP with a need for serial registration of physical products, and this has been an exciting adventure up to now... I just would like to finish it.
      :smiley: :smiley: :smiley:

      Thanks, regards
      Max

  • Jude

    Hi Max,

    Hope you're having a great weekend !!

    I'd be happy to help you complete this

    1) Can you echo the value / log it in a file of the $isowner variable for different users ? Also move it inside the function .

    2) Change assign_level( 1 ) to assign_level( 1, true ) and add some log code inside the add_custom_level function to check if the code does trigger on user updates.

    3) Alternatively try this code to set levels.

    $m = new Membership_Plugin() ;
    
        $member = $m->factory->get_member( $user_id );
        $member->assign_level ( 4 , true ) ;

    Hope that helps

    Cheers
    Jude

    • Max M.

      Hi, and thanks for taking time to aswer during the weekend.
      Very appreciated, really. :slight_smile:

      I'm not familiar with logging, but, in order, I:
      1- manually checked that the related user meta key was correctly saved on the db
      2- moved the $isowner variable in the function
      3- changed the hook to the form submission hook (for good measure)
      3- successfully echoed "Hello World" when the condition is met, thus meaning that everything works except the level assignment

      To make it clear... this gives me a nice "Hello World". :stuck_out_tongue:

      <?php
      /**
       * Plugin Name: Membership level on User Meta by Max
       * Description: Inserisci chi valida il seriale nel gruppo "proprietario verificato"
       */
      
      function add_custom_level() {
      
      $user_ID = get_current_user_id();
      $key = 'unit-verified-owner';
      $single = true;
      
      //step 1 - get value
      $isowner = get_user_meta( $user_ID, $key, $single );
      
      //step 2 - if is owner, give access level which has id number 1
      if ( $isowner == 1 ) {
      echo "Hello World";
          }
      }
      
      //changed to work on submission of related form
      add_action( 'gform_after_submission_5', 'add_custom_level', 10, 1 );

      But the level assignment still doesn't work.
      If I use this code it simply doesn't work, as previously described...

      $m = new Membership_Model_Member($user_id) ;
          $m->assign_level ( 1, true ) ;

      If instead I try this one, the form submission takes FOREVER, the page doesn't reload, and after about one minute I interrupt it, worrying about server overload.

      $m = new Membership_Plugin() ;
      
          $member = $m->factory->get_member( $user_ID );
          $member->assign_level ( 1 , true ) ;

      But this last code at least seems to do something. :stuck_out_tongue:

      Still, when I go on the Members page of the plugin backend, to check if the user has been moved to the level 1, nothing changes.

      Speaking of which... I wonder if this helps you to help me: consider that this form updates an existing user.
      Since we're working on an existing user, we have a user who was already assigned to a level (the standard one for registered users, which is ID=4, FYI).

      Is it possible, by any means, that the issue is here? I mean, maybe there's a different code to move one user from one level to another, and this was conceived to assign a level to a user who doesn't have one yet.

      Just guessing, but I've checked the plugin files and I got that impression.
      Hopefully this info helps! ^_^

      Thanks A LOT for your patience.
      I can't tell you how interesting this challenge is for me, I'm learning quite a lot! :slight_smile:

      Best regards
      Max

  • Jude

    Hi again Max

    Sorry the response took a while longer than expected. Appreciate your patience

    Here is the best way to this. Looks like you'll need to take the subscription into account, only a level associated with that particular sub can be added. This is why we were getting errors.

    add_action( 'user_register',  'new_user_assignment'  , 10, 1 );
    
    function add_custom_level (  $user_id  ) {
            $m = new Membership_Factory()
    			$member = $m->get_member( $user_id );
               $from_sub = $member->on_sub($subscription->id );
               $from_level = $member->on_level($subscription->id ); 
    
               $member->move_subscription($from_sub , $to_sub ,  $to_level , 1 );
               $member->move_level( $from_level, $to_level, $to_sub  ) ;
    }

    Let me know if that helps
    Jude

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.