I have memberships 2 pro installed and imported members

Hi
I have memberships 2 pro installed and imported members across from the original memberships plugin. But I now have a csv of people we are giving a complimentary membership. So I need to import and assign to a specific group. I have tried to use the import tool in Memberships 2 but it seems this may be created to deal with exports created in memberships, not a csv or xls etc.

Is there a tool or script that will import such a csv, and ideally allocate to a group, or if I need to write a script to import, is it a case of just creating users (ie using the default wp import in tools or writing a script), and then binding the new user id and the specific member group id relationship somewhere?
Thanks
Mike

  • Ash

    Hello @bizetools

    Welcome to WPMU community!

    I hope you are well today and thanks for asking the question.

    How did you make that csv? Is it exported from old membership plugin?

    You can use json to import in new membership. If you go to Membership 2 > Settings > Import Tool > Export Data and you will get a json file. Follow that json file, add your additional data (users and complementary membership) and import again.

    Please take a db backup before doing so.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Ash

    Hello @bizetools

    Yes, it should create the relationship. And you need to remove those users if they are already existing.

    If you just want to assign one membership to some (or lots of) existing users, you can do so using the simple code here:

    add_action( 'admin_init', 'add_membership' );
    function add_membership() {
    	$membership_id = 1343;
    	$min_id_range = 1000;
    	$max_id_range = 2000;
    
    	for( $i = $min_id_range; $i <= $max_id_range; $i++ ) {
    		$member = MS_Factory::load( 'MS_Model_Member', $i );
    		$subscription = $member->add_membership( $membership_id );
    
    		if ( $member->has_membership() ) {
    			$member->is_member = true;
    		} else {
    			$member->is_member = false;
    		}
    		$member->save();
    	}
    }

    You will need to provide the membership ID, approximate minimum and maximum ID number of the users.

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • bizetools

    Hopefully last one Ash :slight_smile:
    I have created a plugin folder mu-plugins. I added a doc in there with the following:
    <?php
    add_action( 'admin_init', 'add_membership' );
    function add_membership() {
    $membership_id = 6587; // ID of the membership group
    $min_id_range = 0;
    $max_id_range = 443; // run against all 443 users in the system

    for( $i = $min_id_range; $i <= $max_id_range; $i++ ) {
    $member = MS_Factory::load( 'MS_Model_Member', $i );
    $subscription = $member->add_membership( $membership_id );

    if ( $member->has_membership() ) {
    $member->is_member = true;
    } else {
    $member->is_member = false;
    }
    $member->save();
    }
    }

    ?>

    I must have missed something as it has not applied the membership, and the members that were not allocated to membership group 6587 are still showing as visitor. Any suggestions?
    Thanks

  • Ash

    Hello @bizetools

    I hope you are well today.

    Hi Ash, once this runs the once, would prob then best best to remove. This is just a once off import Im told

    Yes, I should have told you, you need to remove the code when members are added.

    About the mu-plugins issue, did you give the file name as a php file? The file must have .php extension like my-custom-plugins.php

    Please let me know about this.

    Cheers
    Ash

  • Ash

    Hello @Mike

    I hope you are well today.

    Would you please send me admin login details and FTP details?

    To send me details, please use our contact form: https://premium.wpmudev.org/contact/

    Select: I have a different question
    Subject: Attn-Ash (this ensures that it will be assigned to me)
    Details:
    1. Send all requested details
    2. Send a link of this thread so that I can track
    3. Send any other relevant link

    I will be happy to take a look :slight_smile:

    Cheers
    Ash

  • bizetools

    Hi Ash,
    As my client is getting anxious, I have tried the alternative solution of adding code to the bottom of functions:
    add_action( 'admin_init', 'add_membership' );
    function add_membership() {
    $membership_id = 6587;
    $min_id_range = 35;
    $max_id_range = 703;

    for( $i = $min_id_range; $i <= $max_id_range; $i++ ) {
    $member = MS_Factory::load( 'MS_Model_Member', $i );
    $subscription = $member->add_membership( $membership_id );

    if ( $member->has_membership() ) {
    $member->is_member = true;
    } else {
    $member->is_member = false;
    }
    $member->save();
    }
    }

    Unfortunately admin would not load running that function. I then removed from functions.php, and the membership numbers remained unchanged.
    Thanks
    Mike

  • Ash

    Hello @bizetools

    I am sorry for the delay. Please check now. I have added all the user to that membership.

    Used code:

    add_action( 'admin_init', 'add_membership' );
    function add_membership() {
    	$membership_id = 6587;
    	$min_id_range = 312;
    	$max_id_range = 702;
    
    	for( $i = $min_id_range; $i <= $max_id_range; $i++ ) {
    		if( user_id_exists( $i ) ){
    			$member = MS_Factory::load( 'MS_Model_Member', $i );
    			$subscription = $member->add_membership( $membership_id );
    
    			if ( $member->has_membership() ) {
    				$member->is_member = true;
    			} else {
    				$member->is_member = false;
    			}
    			$member->save();
    		}
    	}
    }
    
    function user_id_exists($user){
    
        global $wpdb;
    
        $count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users WHERE ID = '$user'"));
    
        if($count == 1){ return true; }else{ return false; }
    
    }

    Cheers
    Ash