Need assistance with adding bulk users to memberships

I have import all users and memberships, Now I need to add these users to their right memberships. I have a membership that has over 10000 users, the import tool isn't running through that many users at a time. If i tried to separate them into many different files, i will need to clear all data every time i import otherwise there will be duplicated memberships.

I prefer to do some custom coding, I have also looked into M2 plugin file, seems like there's a class and a method to do that, membership/app/model/class-ms-model-relationship.php, create_ms_relationship at line 400

Would like further assistance regarding on how to move forward with custom coding.

  • Katya Tsihotska

    Hi James Wang

    Hope you had a wonderful day!

    Since this is dealing with more advanced coding topics, I've pinged our SLS Team (code experts) to get their valuable feedback on your question. One of our SLS members will reply back here as soon as they can.

    Please keep in mind that our SLS Team deals with more complicated issues, thus it may take a little longer for them to reply here. Thank you for your patience while we look into this further.

    Kind regards,
    Katya

  • Lindeni Mahlalela

    Hello James Wang

    I hope you are doing great today. Thank you for contacting us regarding your needs and we hope to help in any way possible.

    I think the best way to do this is to use a custom Cron Job that will loop through all the members and assign the relevant memberships. I have looked at the code of Membership 2 Pro and found that there are some useful functions you can use to achieve this. Here are the functions you can try:

    assign_memberships( $user_id, $memberships );
    Located in 'membership/app/controller/class-ms-controller-member.php'

    This function takes the user_id and an array of membership ids to be assigned to the user. The function will only assign the memberships included in the $memberships variable, all memberships that are not assigned to this user will be removed, so you should consider adding them as well.

    Sample usage:

    $user_id =get_current_user_id(); //don't use this in production as it will assign all logged in users to the membership even if you don't want to. Good idea if you intend to do so
    $memberships = array('1', '2', '3'); //assign the memberships with ID 1,2 and 3
    MS_Controller_Member::assign_memberships( $user_id, $memberships );

    add_membership( $membership_id, $gateway_id = 'admin', $move_from_id = 0 )
    Located in 'membership/app/model/class-ms-model-member.php'

    Sample usage:

    $member 		= MS_Factory::load( 'MS_Model_Member', $user_id );
    $member->add_membership(1); //to add  membership whose ID is 1

    And the one you found:
    create_ms_relationship($membership_id = 0 , $user_id = 0, $gateway = 'admin', $move_from_id=0);
    membership/app/model/class-ms-model-relationship.php, create_ms_relationship

    Sample usage:
    MS_Model_Relationship::create_ms_relationship(1, 10); //user:1, membership:10, default gateway:admin

    This one is similar to the 'add_membership()' with the exception that it also require the user ID but it works the same.

    You may also want to explore the API if you prefer to use advanced featured, the documentation is available in "Membership 2 > Help > API Docs" page. It has some useful functions that can be used for this purpose.

    To make things a bit easier for you, I have created a template plugin that has the following ability:

    - Define custom cron schedule (perminute)
    - Define and add custom cron job to run every minute. This is useful for large number of users
    - Assign members to a membership(s) every minute using custom cron job
    - Re schedule cron job if not done
    - Remove cron schedule if done

    You can find the plugin attached to this post. You may install it as a regular WordPress plugin by uploading it via "Plugins > Add New" page. Before you pload, you need to extract/unzip and edit the php file to change the following lines in the construct function:

    $this->how_many = 100; //number of users per batch
    $this->memberships = array('1', '2');  //aray of memberships to assign the users to.

    Also you have to edit the function that gets the users so that it can apply any selection criteria that you want to use. I am sure if you have so many users you don't want to apply them all to the same membership so be careful with this.

    I suggest you use it in a testing environment to ensure it does what you want to do. Of course you can expand it to add any advanced features to it like add an admin page that will allow you to choose which memberships to assign and which users to assign to. You may also add a custom meta to the users and use this meta to choose the users, say for example add a meta value for the membership name and use this custom meta to select all users with this custom meta and assign the membership accordingly. Another way, if you have the users in a CSV file you can use the custom cron to read the file and assign the membership based on a column with the membership for that user.

    I hope this helps. Please let us know if you need any further assistance with regards to this.

    Have a nice day.
    Mahlamusa

  • Lindeni Mahlalela

    Hello James Wang

    I hope you are doing great today. My colleague has pointed out a typo in the code. Could you please check and fix the code as follows:

    On line 65 change the function name from:

    public function ms_get_wordpress_users(){

    To:

    public function ms_get_wp_users(){

    Otherwise the code will not work. You may also download the attached zip file with the fix applied.

    I hope this helps. Please let us know if you need any further assistance.

    Have a nice day.
    Mahlamusa

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.