Membership 2 Pro - need an action whenever memberships are updated (for gifting memberships)

I want to implement gifting memberships. In short, I'm trying to hook into the process of getting a new membership.
When the membership has a specific id the membership is removed from the user who bought it, and its id gets stored in combination with a token in my db.
Whenever you call a specific URL (with the generated token) the membership id is transferred to the user who called the URL.

Currently, I'm hooking into the init action and check if ms_relationship_id is in the URL. but there's probably an action which is called after a membership is bought.

Tried around ~20 actions yesterday, but there are no docs and things like save_event, ... didn't work.

And ofc I need to disable the default mail for this membership, since "your membership is now activated" is simply wrong in this case.

  • Katya Tsihotska

    Hi loremipsum

    Hope you're having a wonderful day!

    Since this is dealing with more advanced coding topics, I'm going to ping our SLS Team (code experts) to get their valuable feedback on this. One of our SLS members will reply back here as soon as more information is available.

    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

  • Panos

    Hi loremipsum ,

    Cool username btw :slight_smile:

    I have 2 suggestions.
    1st. Can be used for frontend only where you can use the
    ms_controller_frontend_payment_table
    like this:

    add_filter( 'ms_controller_frontend_payment_table' , function( $html ){
    
    			$membership_id 	= intval( $_REQUEST['membership_id'] );
    			$membership 	= MS_Factory::load( 'MS_Model_Membership', $membership_id );
    			$member 		= MS_Model_Member::get_current_member();
    //Do stuff
    
    return $html;
    }, 10 )

    2nd. This can be used also when creating subscriptions from admin:
    ms_model_relationship_create_ms_relationship
    and can be used like:

    add_filter( 'ms_model_relationship_create_ms_relationship', function( $subscription, $membership_id, $user_id, $gateway_id, $move_from_id ){
    
        //Do stuff with $membership_id
        // to $user_id
        //if ( $subscription->status == MS_Model_Relationship::STATUS_ACTIVE )
    
        return $subscription;
    
    }, 10, 5 );

    Hope this helps :slight_smile:

  • loremipsum

    This action is already called when a membership is selected (before paying it). I need to hook into the process of applying the membership to the member.

    The exact process of gifting membership is:

    - when a specific membership id is purchased the membership is removed from the user who bought it and stored in a custom post type (with a random generated token)
    - when you call a specific url with the token the membership is transfered to the user who called the url

    By the way: what action to hook in to prevent / change the mail when purchasing a membership? Gifting memberships should not send the default mail but show you the token and the url you can send to the receiver.

  • Panos

    If you mean before even clicking, so before the membership is assigned to member, an appropriate action would be the following :

    add_action( 'ms_view_frontend_payment_form_start', function( $invoice, $front ){
    
        $membership_id = $invoice->membership_id;
        $subscription_id = $invoice->ms_relationship_id;
        $user_id = $invoice->user_id;
        $subscription = MS_Model_Relationship::get_subscription(
                    $user_id,
                    $membership_id
                );
    
    }, 10, 2 );

    Cheers!

  • Panos

    From your description it seems that one of the previous hooks should work for your needs
    ms_controller_frontend_payment_table
    or
    ms_model_relationship_create_ms_relationship
    where you can check if the subscription status is active, which means already paid.

    For the "Thank you" page, you can use the :

    add_action( 'ms_controller_gateway_process_purchase_after', function( $gateway_controller ){
    
        $subscription = MS_Factory::load(
            'MS_Model_Relationship',
            $_REQUEST['ms_relationship_id']
        );
    
        $membership = $subscription->get_membership();
    
        $member = MS_Model_Member::get_current_member();
    
        //You have membership and member to do what you need to do
    
    }, 10 );

    Personally I would recommend one of the 2 first ones.

    You should test them all though and decide which one fits your needs best :slight_smile:

    Regarding the question about the email, it initially uses the action
    ms_model_event
    so first option here would be to remove action and add a new custom function for this action.

    Second and (imo) simpler would be to use one of these two filters:
    ms_model_communication_send_message_recipients
    and
    ms_model_communication_send_message_contents

    You can use the first one to return and empty array in order not send the notification at all. Or, you can use the second one to change the content of the message

    Here is an example of the second filter:

    add_filter( 'ms_model_communication_send_message_contents', function( $message, $communication_model, $subscription, $content_type ){
    
        if( $communication_model->type == $communication_model::COMM_TYPE_INVOICE ){
    
            if( some check ){
                $message = 'A new message';
            }        
    
        }
    
        return $message;
    
    }, 10, 4 );

    Hope this helps!

    Cheers!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.