Webhooks for Membership Changes

I am trying to synchronize a custom post type with membership pro so that my client can use their site like a CRM. They want to be able to view and manage all of their member data via the custom post types as opposed to within each individual plugin.

I have already written a filter for the custom post type so that the admin can change the membership level via the post page as opposed to within membership 2. However, I need to write some code so that the value in the custom post type will change when changes are made via the membership plugin.

For example, when a subscription expires and when a subscription is renewed, i would like to write some code to update the meta value for the custom post type.

Can you tell me where I can find this trigger/hook?

  • Jude

    Howdy Nick

    Membership 2 comes with a Events class that can be of great help here. This action hook should alert you to all possible combination of events.

    do_action( 'ms_model_event', $event, $data );

    Make sure you're listening in to all Membership related events ( there are a bunch of others too ). The $data variable contains everything you need for your custom work. A var_dump should show you its contents.

    Please take a couple of mins and review this file. It has all the needed good stuff.

    wp-content/plugins/membership/app/model/class-ms-model-event.php

    Cheers
    Jude

  • Nick

    I have written the following code in my functions.php file:

    function senddata($event, $data) {
    	mail('me@mydomain.com','test send of data',$event);
    }
    add_action('ms_model_event', 'senddata');

    It is sending the email when I add or remove a membership level, but there is no data coming through for the $event. I have also tried serializing $data with no results either.

  • Ivan Shulev

    Hey Nick ,

    I hope you are having a nice day so far!

    The way you wrote the add_action hook, you are only getting the $event variable. You need to specify the number of parameters. It is 1 by default.

    Please try the code like so:

    function senddata($event, $data) {
    	mail('me@mydomain.com','test send of data',$event);
    }
    add_action('ms_model_event', 'senddata', 10, 2);

    I hope this helps and I wish you an awesome day ahead!

    Ivan

  • Nick

    OK, tried that. When I do that and serialize the results of $data, this is what I get:

    O:21:"MS_Model_Relationship":26:{s:16:" * membership_id";s:5:"12520";s:13:" * gateway_id";s:5:"admin";s:13:" * start_date";s:10:"2016-01-01";s:14:" * expire_date";s:10:"2017-01-01";s:20:" * trial_expire_date";s:0:"";s:25:" * trial_period_completed";b:1;s:9:" * status";s:6:"active";s:25:" * current_invoice_number";s:1:"1";s:15:" * move_from_id";i:0;s:24:" * cancelled_memberships";s:0:"";s:9:" * source";s:0:"";s:12:" * source_id";s:0:"";s:11:" * payments";a:0:{}s:15:" * is_simulated";s:0:"";s:15:" * payment_type";s:10:"date-range";s:33:" MS_Model_Relationship membership";O:19:"MS_Model_Membership":27:{s:5:" * id";i:12520;s:7:" * name";s:20:"Affiliate Membership";s:8:" * title";s:0:"";s:14:" * description";s:0:"";s:14:" * rule_values";a:2:{s:4:"page";a:10:{i:0;i:11835;i:1;i:11831;i:2;i:11839;i:3;i:11843;i:4;i:11841;i:5;i:11845;i:6;i:11837;i:7;i:27964;i:8;i:28013;i:9;i:28024;}s:4:"menu";a:9:{i:0;i:12201;i:1;i:12202;i:2;i:12203;i:3;i:12206;i:4;i:12205;i:5;i:12204;i:6;i:12207;i:7;i:12231;i
    :8;i:12181;}}s:7:" * type";s:6:"simple";s:15:" * payment_type";s:10:"date-range";s:9:" * active";s:1:"1";s:10:" * private";s:1:"1";s:10:" * is_free";s:0:"";s:20:" * disabled_gateways";a:0:{}s:8:" * price";s:3:"125";s:9:" * period";a:2:{s:11:"period_unit";i:1;s:11:"period_type";s:4:"days";}s:19:" * pay_cycle_period";a:2:{s:11:"period_unit";i:1;s:11:"period_type";s:4:"days";}s:24:" * pay_cycle_repetitions";s:1:"0";s:20:" * period_date_start";s:10:"2016-01-01";s:18:" * period_date_end";s:10:"2017-01-01";s:23:" * trial_period_enabled";s:0:"";s:14:" * trial_price";s:1:"0";s:15:" * trial_period";a:2:{s:11:"period_unit";i:1;s:11:"period_type";s:4:"days";}s:23:" * on_end_membership_id";s:5:"12469";s:21:" * is_setup_completed";s:0:"";s:9:" * source";s:0:"";s:12:" * source_id";s:0:"";s:14:" * custom_data";a:0:{}s:16:" * update_denied";a:0:{}s:17:" * update_replace";a:0:{}}s:5:" * id";i:40937;s:7:" * name";s:34:"user_id: 997, membership_id: 12520";s:8:" * title";s:0:"";s:14:" * description";s:
    34:"user_id: 997, membership_id: 12520";s:10:" * user_id";s:3:"997";s:16:" * post_modified";s:19:"2016-05-18 12:13:31";s:14:" * custom_data";a:0:{}s:9:"_in_cache";b:1;s:11:"_saved_data";a:22:{s:13:"membership_id";s:5:"12520";s:10:"gateway_id";s:5:"admin";s:10:"start_date";s:10:"2016-01-01";s:11:"expire_date";s:10:"2017-01-01";s:17:"trial_expire_date";s:0:"";s:22:"trial_period_completed";s:1:"1";s:6:"status";s:11:"deactivated";s:22:"current_invoice_number";s:1:"1";s:12:"move_from_id";s:1:"0";s:21:"cancelled_memberships";s:0:"";s:6:"source";s:0:"";s:9:"source_id";s:0:"";s:8:"payments";a:0:{}s:12:"is_simulated";s:0:"";s:12:"payment_type";s:10:"date-range";s:2:"id";i:40937;s:4:"name";s:34:"user_id: 997, membership_id: 12520";s:5:"title";s:0:"";s:11:"description";s:34:"user_id: 997, membership_id: 12520";s:7:"user_id";s:3:"997";s:13:"post_modified";s:19:"2016-05-18 12:13:31";s:11:"custom_data";a:0:{}}s:14:" * _factory_id";s:41:"ms_model_relationship-40937-573c5c761ea7e";}

    When i try to unserialize that, or parse $data as an array, it produces an error because it says it is not a proper array.

  • Ivan Shulev

    Hey Nick ,

    It looks like an instance of the MS_Model_Relationship class.

    You can add the following to your code to make sense of it:

    if ( $data instanceof MS_Model_Relationship ) {
    	$subscription = $data;
    	$membership = $subscription->get_membership();
    	$member = MS_Factory::load(
    		'MS_Model_Member',
    		$subscription->user_id
    	);
    	$user_id = $subscription->user_id;
    	$membership_id = $subscription->membership_id;
    	$ms_relationship_id = $subscription->id;
    	$name = sprintf(
    		'user: %s, membership: %s, type: %s',
    		$member->name,
    		$membership->name,
    		$type
    	);
    }

    You can have a look at the MS_Model_Relationship class in your /wp-content/plugins/membership/app/model/class-ms-model-relationship.php file.

    I hope this helps and I wish you an awesome day ahead!

    Ivan