[Membership 2 Pro] Update MailChimp on Membership update

We are able to add a user to a MailChimp list and set a certain status when a member signs up in Membership 2 Pro.

But we are looking for a way to modify/change that status when there are changes to their subscription, e.g. they update their membership or unsubscribe.

Please take a look at this comment here for details – https://premium.wpmudev.org/forums/topic/in-addition#post-1407919. Is there a hook or snippet available to achieve this?

Please advice.

  • Panos
    • SLS

    Hi stripedrhino !

    As Ash mention, most of the status changes can be monitored in that hook. You mention the term unsubscribe which is a but blur to me. If by that you mean cancelled then you should be fine using that hook. However when a member cancels, the subscription is still active until expiration date. At that date the subscription status gets set to deactivated

    If what you want is the second, it is best to do this check through the cron status check, but it is quite complex. I have prepared a snippet that should change a “MMERGE” var when the subscription gets deactivated/expired. You can download it from here :

    https://gist.github.com/wpmudev-sls/02ec5b583432259b837382aff16d01f3

    After you download and unzip it, you can open the ms-mailchimp-segment-vars.php file with a text/code editor. You can set the “MMERGE” variables in this part of the file:

    self::$merge_fields =   [
    //'FNAME' => 'Jack',
    //'LNAME' => 'White',
    'MMERGE5' => 'The new value'
    ];

    The above should set the “MMERGE5” segment to “The new value” for any subscription that it’s new status is going to be set to expired/deactivated.

    Once you make your changes there, you can upload that file to the wp-content/mu-plugins folder.

    Now in order to test, you can create new test subscriptions and then from the admin set the start and end dates to the past (at least 2-3 days back though, cause of the grace period). Once you set that, you can go to the plugin’s settings page and force run the status cron check by clicking on the link at the Check Membership Status changes footer.

    I would strongly suggest to first try this out on a staging or local site before moving it to the production site.

    Hope this helps!

    Kind regards!

  • stripedrhino
    • Site Builder, Child of Zeus

    Hello Panos

    Thanks for you replay..

    Actually i want to setup customer into mailchimp using membership 2 pro

    please look into screenshots

    In screenshot 1 : i will add a member and redirect to screenshot 2

    In screenshot 2 : i will select random multiple plans or single plan but it's not effect on mailchimp as per i want like in screenshot 3

    i have used this code in function.php for add selected subscription details in mailchimp add_filter( 'ms_addon_mailchimp_subscribe_user_merge_vars', function( $merge_vars, $member, $list_id ) {}

    but it's only work when i get user from search bar and update plans like in this below screenshot

    But it only work 1st time after this code not work ..

    so can you please help me to get out of this situation..

    Thanks

  • Panos
    • SLS

    Hi stripedrhino !

    In regards to the case where you are adding memberships from the admin page to a member, those events are not used in the MailChimp add-on. I think we could statically call the addon’s method from that event, and since you check Membership id in the $_REQUEST global (or at least this is what I assume), we could set that in the ms_model_event_signed_up as an example :

    add_action( 'ms_model_event_signed_up', function( $event, $data ){

    if ( ! isset( $_POST['action'] ) || 'member_subscription' != $_POST['action'] ) {
    return;
    }

    $new_membership_id = isset( $_POST[ 'subscribe' ] ) && is_array( $_POST[ 'subscribe' ] ) ? $_POST[ 'subscribe' ][0] : false;

    if ( ! $new_membership_id ) {
    return;
    }

    $_REQUEST[ 'membership_id' ] = $new_membership_id;
    $MS_Addon_Mailchimp = new MS_Addon_Mailchimp();

    $MS_Addon_Mailchimp->subscribe_members( $event, $data );

    }, 20, 2 );

    This should trigger MS_Addon_Mailchimp->subscribe_members which should reach the ms_addon_mailchimp_subscribe_user_merge_vars. Just make sure that you set this part $_REQUEST[ 'membership_id' ] = $new_membership_id; to match the way you get the membership id.

    As for the second request, not sure how to fetch the plans’ details. Are those from the registration form? Are those custom fields added with custom code? Could share more info about the plan details?

    For now I’ll continue assuming you already have these details. From the screenshots I understand that you want to print those details in the checkout page. Depending on where you prefer to print those here are 3 different places. I’ll use static content in following examples:

    Inside checkout table, in a new table row after total:

    add_action( 'ms_view_frontend_payment_after_total_row', function(){

    $the_list = '<ul>';
    $the_list .= '<li>Item one</li>';
    $the_list .= '<li>Item two</li>';
    $the_list .= '</ul>';
    ?>
    <tr>
    <td>
    Some label
    </td>
    <td>
    <?php echo $the_list; ?>
    </td>
    </tr>

    <?php
    } );

    Inside checkout table, in the same row and right above the description (last row):

    if ( ! function_exists( 'get_ms_pm_invoice_payment_description' ) ) {
    function get_ms_pm_invoice_payment_description() {

    $the_list = '<ul>';
    $the_list .= '<li>Item one</li>';
    $the_list .= '<li>Item two</li>';
    $the_list .= '</ul>';

    $out = "<div>{$the_list}</div>";
    $out .= MS_Helper_Template::$ms_front_payment['invoice_payment_description'];

    return $out;
    }
    }

    After the table:

    add_action( 'ms_view_frontend_payment_after', function(){
    ?>
    <h5>After table</h5>
    <?php
    } );

    In case I understood wrong please let me know.

    Kind regards!

  • stripedrhino
    • Site Builder, Child of Zeus

    Hello Panos

    Thanks for your reply

    Actually i have this type of code for mailchimp MERGE variable

    $membership_id = null;
    $purchased_plan_id = array();

    $subscribe_ids = $_POST['subscribe'];
    $selected_plan = get_user_meta($member,'ms_subscriptions',false);

    ///////// define all membership's id in array//////////
    if(isset($selected_plan) && !empty($selected_plan) && !empty($selected_plan[0])){
    foreach ($selected_plan[0] as $key => $purchased_plan) {
    if($key>0){
    $purchased_plan_id[] = $purchased_plan->membership_id;
    }
    }
    }

    if(isset($subscribe_ids)){
    foreach ($subscribe_ids as $membership_id) {
    ////////check membership id have subscription or otherwise send to unsubscribe
    if ( 30821 == $membership_id ) {
    $merge_vars['MMERGE11'] = 'MAT-WEEKLY';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30821 != $membership_id){
    $merge_vars['MMERGE11'] = 'MAT-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 37951 == $membership_id ) {
    $merge_vars['MMERGE14'] = 'REFORMER-WEEKLY';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 37951 != $membership_id){
    $merge_vars['MMERGE14'] = 'REFORMER-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 37953 == $membership_id ) {
    $merge_vars['MMERGE17'] = 'REFORMER-ANNUALLY';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 37953 != $membership_id){
    $merge_vars['MMERGE17'] = 'REFORMER-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 35894 == $membership_id ) {
    $merge_vars['MMERGE18'] = 'MAT-ANNUALLY';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 35894 != $membership_id){
    $merge_vars['MMERGE18'] = 'MAT-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 42198 == $membership_id ) {
    $merge_vars['MMERGE15'] = 'MAT-INTRO';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 42198 != $membership_id){
    $merge_vars['MMERGE15'] = '';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 30822 == $membership_id ) {
    $merge_vars['MMERGE19'] = 'AGENCY';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30822 != $membership_id){
    $merge_vars['MMERGE19'] = 'REFORMER-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 30823 == $membership_id ) {
    $merge_vars['MMERGE20'] = 'MAT-TRIAL';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30823 != $membership_id){
    $merge_vars['MMERGE20'] = 'MAT-TRIAL-ENDED';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 31095 == $membership_id ) {
    $merge_vars['MMERGE10'] = 'MAT-VIP';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 31095 != $membership_id){
    $merge_vars['MMERGE10'] = 'MAT-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 30665 == $membership_id ) {
    $merge_vars['MMERGE22'] = 'MAUI-WORKSHOP';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30665 != $membership_id){
    $merge_vars['MMERGE22'] = '';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 30677 == $membership_id ) {
    $merge_vars['MMERGE23'] = 'MAUI-WORKSHOP';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30678 != $membership_id){
    $merge_vars['MMERGE23'] = '';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 30678 == $membership_id ) {
    $merge_vars['MMERGE24'] = 'MAUI-MAT';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30678 != $membership_id){
    $merge_vars['MMERGE24'] = '';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 45079 == $membership_id ) {
    $merge_vars['MMERGE21'] = 'MAT-REFERRED';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 45079 != $membership_id){
    $merge_vars['MMERGE21'] = 'MAT-UNSUBSCRIBE';
    $merge_vars['MMERGE8'] = 'NO';
    }

    if ( 30679 == $membership_id ) {
    $merge_vars['MMERGE25'] = 'MAUI-MAT-WORKSHOPS';
    $merge_vars['MMERGE8'] = 'YES';
    }elseif(in_array($membership_id, $purchased_plan_id) && 30679 != $membership_id){
    $merge_vars['MMERGE25'] = '';
    $merge_vars['MMERGE8'] = 'NO';
    }
    }
    }

    so how should i have manage in add_action( 'ms_model_event_signed_up', function( $event, $data ){} function.

    i have tried my code into this function but nothing happen..

    so please help regarding to this..

    Thanks

  • Panos
    • SLS

    Hi stripedrhino ,

    The snippet shared above should manually call the Mailchimp’s subscribe_members() function. You can keep your code as previously in the ms_addon_mailchimp_subscribe_user_merge_vars filter, no need to change that. Actually I just tested and don’t even need my snippet provided above.

    If you could share admin and ftp access to a staging site, I could try out your code there. If you’re interest you can share this info privately through our contact form: https://premium.wpmudev.org/contact/#i-have-a-different-question

    Send in:Subject: “Attn: Panos Lyrakis”

    – Admin login:

    Admin username

    Admin password

    Login url

    – FTP credentials

    host

    username

    password

    (and port if required)

    – link back to this thread for reference

    Kind regards!

  • Panos
    • SLS

    Hey stripedrhino !

    Thanks for sending those in!

    When adding a new membership to a member from the admin, the filter ms_addon_mailchimp_subscribe_user_merge_vars is actually reached, and I see that depending on which membership is selected I get different values in the merged variable.

    Are you trying to add more that one memberships to a member? If this is the case this happens because the Mailchimp add-on checks:

    if ( ! $already_subscribed ) {

    I did a minor change in the add-on’s file and I see that the filter is reached when adding multiple memberships to a member. One note though, currently when adding the new member in a Mailchimp list, it removes the member from the current one. If you want the member to be in multiple lists too let me know so I could change that too.

    Please make a few tests and let me know how it goes.

    Kind regards!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.