Get Expired user list from Membeship 2 pro

How can I get the expired users list? Is there any hook or action available on the user membership expiration event so as soon as users membership get expired I can call hook or action to update their portfolio post as a draft status?

  • Kasia Swiderska

    Hello Nickolas,

    I will ping developer for his feedback to point you in right direction with it. There is conditional tag ms_has_membership() that returns true or false depending if user has membership/s , but I'm not sure it can be used for checking all the members.
    I also found in API documentation get_members() method that will get members list of this membership. This will also count members that have "cancelled" or "expired" subscriptions but not "pending" or "deactivated".
    And it also has filter parameter:

    $args = array( 'status' => 'all' )

    You can check it in the API documentation on site where you have Membership 2 Pro installed /wp-content/plugins/membership/docs/classes/MS_Model_Membership.html#method_get_members

    kind regards,
    Kasia

  • Ash

    Hi Nickolas

    Here is a snippet you can try:

    add_action( 'ms_model_event_save_event', function( $event, $type, $data ) {
        if( MS_Model_Event::TYPE_MS_EXPIRED == $type )
        {
            $user_id = $event->user_id;
            $member = MS_Factory::load(
                'MS_Model_Member',
                $user_id
            );
    
            // Now you have $user_id and $member
            // Do whatever you want :)
    
        }
    }, 20, 3 );

    Here you have user ID and member object, so you can do whatever you want.

    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

  • Ash

    Yes, there are list of type constants you can check :slight_smile:

    Here are those:

    const TYPE_UPDATED_INFO = 'updated_info';
    const TYPE_MS_SIGNED_UP = 'signed_up';
    const TYPE_MS_MOVED = 'moved';
    const TYPE_MS_EXPIRED = 'expired';
    const TYPE_MS_TRIAL_EXPIRED = 'trial_expired';
    const TYPE_MS_DROPPED = 'dropped';
    const TYPE_MS_RENEWED = 'renewed';
    const TYPE_MS_DEACTIVATED = 'deactivated';
    const TYPE_MS_CANCELED = 'canceled';
    const TYPE_MS_REGISTERED = 'registered';
    const TYPE_MS_RESETPASSWORD = 'resetpassword';
    const TYPE_MS_BEFORE_FINISHES = 'before_finishes';
    const TYPE_MS_AFTER_FINISHES = 'after_finishes';
    const TYPE_MS_BEFORE_TRIAL_FINISHES = 'before_trial_finishes';
    const TYPE_MS_TRIAL_FINISHED = 'trial_finished';
    const TYPE_CREDIT_CARD_EXPIRE = 'credit_card_expire';
    const TYPE_PAID = 'paid';
    const TYPE_PAYMENT_FAILED = 'payment_failed';
    const TYPE_PAYMENT_PENDING = 'payment_pending';
    const TYPE_PAYMENT_DENIED = 'payment_denied';
    const TYPE_PAYMENT_BEFORE_DUE = 'payment_before_due';
    const TYPE_PAYMENT_AFTER_DUE = 'payment_after_made';

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

    Cheers
    Ash

  • Nickolas

    Hi Ash,

    I am trying to put my custom post portfolio in draft mode after member is expire
    But none of the event occur, I have written below code for expire event with portfolio draft mode code. Can u look at this event

    add_action( 'ms_model_event_save_event', function( $event, $type, $data ) {
    if( MS_Model_Event::TYPE_MS_EXPIRED == $type )
    {
    $user_id = $event->user_id;
    $member = MS_Factory::load(
    'MS_Model_Member',
    $user_id
    );

    // Now you have $user_id and $member
    // Do whatever you want :slight_smile:

    if (get_user_meta($event->user_id, 'portfolio',true) > 1 )
    {
    $portfolio = array(
    'ID' => get_user_meta($event->user_id, 'portfolio',true),
    'post_status' => 'draft',
    );
    wp_update_post( $portfolio );
    }

    }
    }, 20, 3 );

  • Nickolas

    Hello,

    Do you have any hook that can be useful to use on Membership payment event? When does "ms_model_event_save_event" fired?

    Actually, I am trying to put my custom post "portfolio" members post in draft mode after particular membership is expired and making it "published" once they renew the membership.

    As suggested above, I had tried "ms_model_event_save_event" but it doesn't seem to be working. See below mentioned code:

    /// Set Portfolio Status after Expire or Membership Active.
    add_action( 'ms_model_event_save_event', function( $event, $type, $data ) {
    if( MS_Model_Event::TYPE_MS_EXPIRED == $type )
    {
    $user_id = $event->user_id;
    $member = MS_Factory::load(
    'MS_Model_Member',
    $user_id
    );

    $getusermeta = get_user_meta($user_id, 'portfolio',true);
    if($getusermeta)
    {
    $portfolio = array(
    'ID' => get_user_meta($user_id, 'portfolio',true),
    'post_status' => 'draft',
    );
    wp_update_post( $portfolio );
    }
    }

    if( MS_Model_Event::TYPE_MS_RENEWED == $type )
    {
    $user_id = $event->user_id;
    $member = MS_Factory::load(
    'MS_Model_Member',
    $user_id
    );

    $getusermeta = get_user_meta($user_id, 'portfolio',true);
    if($getusermeta)
    {
    $portfolio = array(
    'ID' => get_user_meta($user_id, 'portfolio',true),
    'post_status' => 'publish',
    );
    wp_update_post( $portfolio );
    }
    }
    }, 20, 3 );

    Can you please help me out here?

    Thanks

  • Kasia Swiderska

    Hello Nickolas,

    Please take a look on this example code - what it does it changes role of user when subscription is deactivated or cancelled. Can you check if using ms_model_event will work better for you?

    add_action( 'ms_model_event', 'my_event_handler', 10, 2 );
    
    /**
     * Handles an event and process the correct communication if required.
     *
     * param  MS_Model_Event $event The event that is processed.
     * param  mixed $data The data passed to the event handler.
     */
    function my_event_handler( $event, $data ) {
    	$member = false;
    	$subscription = false;
    	$membership = false;
    
    	switch ( $event->type ) {
    		case MS_Model_Event::TYPE_MS_CANCELED:
    			// A membership was cancelled - either by Admin or by the member.
    			// No more payments will be made but member has access until current period ends.
    			$subscription = $data;
    			$membership = $data->get_membership();
    			$member = $subscription->get_member();
    			assign_default_role( $member->id );
    			break;
    		case MS_Model_Event::TYPE_MS_DEACTIVATED:
    			// A membership was permanently deactivated. Member has no access anymore.
    			$subscription = $data;
    			$membership = $data->get_membership();
    			$member = $subscription->get_member();
    			assign_default_role( $member->id );
    			break;
    	}
    
    }
    function assign_default_role( $user_id = 0 ){
    	$user = new WP_User( $user_id );
    	$user->set_role( 'subscriber' );
    }

    kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.