Upgrade/downgrade Membership

I integrated Membership 2 with a CRM and wanted to to a add tag in "Upgrade" or "Downgrade" on the CRM side when the user get downgrade or upgrade their membership.

What is the right hook/action to be used?

  • Adam Czajczyk

    Hi Hazem Tasmem,

    I hope you're well today and thank you for your question!

    I believe that would be possible either via Membership 2 API or simply by hooking to some specific action. However, I'll need a consultation from developers on that so I asked them a question and am awaiting their advice.

    Please keep an eye on this ticket and I'll update it as soon as I get a replay,

    Best regards,
    Adam

  • Adam Czajczyk

    Hello again @it54462!

    I just got a suggesting on this. There's a ms_model_member_add_membership hook that should be helpful here. if there's no "Multiple memberships" add-on enabled and user is simply changing membership that should be fired up and you could trigger your own code with it.

    A usage example could be:

    add_filter( 'ms_model_member_add_membership', function( $subscription, $membership_id, $gateway_id, $move_from_id, $member ){
    
        //Do something - your own code here
    
        //Don't forget to return :
        return $subscription;
    }, 10, 5 );

    Best regards,
    Adam

  • Adam Czajczyk

    Hello Knawat.com

    Thank you for your response.

    Just to make sure - this is about the site that you have assigned to this ticket, right? If yes, I noticed that the Membership 2 Pro that seems to be installed there is an outdated 1.1.1 version while current release is 1.1.4. Did you test this with the most recent 1.1.4 then or that older one?

    Also, could you share an example code of yours that you tested? I'd then pass that to our developers to ask them for further advice (or - if necessary - so they could fix that in plugin code).

    Best regards,
    Adam

  • Knawat.com

    Hello Adam Czajczyk,

    Thanks for response.

    Issue is on website https://knawat.com/ and v1.1.2 there. I also have tested with v1.1.4 in my local development server. same behavior. Code I am using is provided by you earlier.

    add_filter( 'ms_model_member_add_membership', function( $subscription, $membership_id, $gateway_id, $move_from_id, $member ){
    
        //Do something - your own code here
    
        //Don't forget to return :
        return $subscription;
    }, 10, 5 );

    I got 0 in $move_from_id every time I had done few test but all has same results.

    Can you please ask to dev team for take a look into this.

    Thanks,

  • Adam Czajczyk

    Hello Knawat.com

    Thank you for your response.

    I checked the plugin's code and from what I understand I think you're right. It seems that the purpose of the $move_from_id variable is a bit different and it doesn't return "old membership" ID "on its own".

    There's another hook (action this time) that I found that - by its name - seem relevant but I admit I'm not sure when exactly it is triggered as it only seems to be executed in one place which doesn't seem to be related to any "downgrade/upgrade" except from admin manually moving memberships. But you might want to give it a try just in case:

    add_action(	'ms_model_membership_move_membership', function($old_membership_id,$new_membership_id,$member) {
    
    		// your own code here
    }, 10, 3);

    I have, however, passed your question again to our developers so please keep an eye on this ticket for further information.

    Kind regards,
    Adam

  • Knawat.com

    Hello Adam Czajczyk ,

    Thanks for reply and suggest hook, This action hook ms_model_membership_move_membership is get called in move_membership and I had already tried it earlier and it not helps. you can check in above reply in this thread. I have mentioned that "move_membership" is not helping.

    Thanks for your information. let me know if you found anything other which is useful or dev team's comment.

    Thanks,

  • Adam Czajczyk

    Hello Knawat.com

    Thanks for your response.

    In this case we'll have to wait for our 2nd line support/developers to suggest better options. Since I already reported it to them (as mentioned in my previous post), I believe they'll be able to share working solution with you. I'd just appreciate some more patience as their response time might be a bit longer than mine here on forum (they deal with a lot of complex issues on daily basis).

    We'll keep you posted here.

    Best regards,
    Adam

  • Panos

    Hi there Knawat.com ,

    The move variables are set only when in the membership's access options there is another membership chosen to change to when it's subscription ends.

    For your request it would require some custom solution on this as plugin has not been designed for that. If the Multiple Memberships add-on is not active and members can only have 1 membership active, I would suggest to try out the following as a guide :

    if ( ! class_exists( 'MS_Record_Membership_Switch' ) ) {
    
    	class MS_Record_Membership_Switch {
    
    		private static $_instance = null;
    		private $membership_from = null;
    
    		public static function get_instance() {
    
    			if ( is_null( self::$_instance ) ) {
    				self::$_instance = new MS_Record_Membership_Switch();
    			}
    
    			return self::$_instance;
    		}
    
    		private function __construct() {
    
    			if ( ! class_exists( 'MS_Model_Addon' ) || MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MULTI_MEMBERSHIPS ) ) {
    				return;
    			}
    
    			add_action( 'ms_model_relationship_create_ms_relationship_before', array( &$this, 'existing_membership' ), 20, 3 );
    			add_action( 'ms_model_relationship_create_ms_relationship', array( &$this, 'record_old_membership' ), 20, 4 );
    
    		}
    
    		function existing_membership( $membership_id, $user_id, $gateway_id ) {
    
    			$member = MS_Factory::load( 'MS_Model_Member', $user_id );
    			$membership_ids = $member->get_membership_ids();
    			$this->membership_from = isset( $membership_ids[0] ) ? $membership_ids[0] : null;
    
    		}
    
    		function record_old_membership( $subscription, $membership_id, $user_id, $gateway_id ) {
    			if ( ! is_null( $this->membership_from ) ) {
    				// DO STUFF
    			}
    		}
    
    	}
    
    	if ( ! function_exists( 'ms_record_membership_switch' ) ) {
    
    		function ms_record_membership_switch() {
    			return MS_Record_Membership_Switch::get_instance();
    		}
    		add_action( 'plugins_loaded', function() {
    			ms_record_membership_switch();
    		});
    
    	}
    
    }

    You can try by replacing
    // DO STUFF
    with some test code you want to use.

    Hope this helps :slight_smile:

    Cheers!

  • Knawat.com

    Hello Panos,

    Thanks for sharing custom code with. It helped me a lot and I am able to achieve my requirements with this. just made below changes in code you provided.

    Replaced line
    add_action( 'ms_model_relationship_create_ms_relationship', array( &$this, 'record_old_membership' ), 20, 4 ); with add_filter( 'ms_model_relationship_create_ms_relationship', array( &$this, 'record_old_membership' ), 20, 4 );

    And added below line after // DO STUFF.
    return $subscription

    Thanks a lot for your valuable time and efforts.

    Regards,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.