Need help with M2P API functions to get member data in PHP

I'm needing some advice and examples of how to get certain membership user data for membership 2 users.

Such as the membership names they are subscribed to and specifically would like to be able to display which coupon they used when they signed up, so we can echo these in a template.

Live Support was able to find this thread which helps with obtaining the membership name, but we have no idea of exactly how to get other days such as coupons they used etc.

  • Peter

    All I need is coupon information. I want to echo it in functions.php. Where is it located? At the moment I have this code for echoing the membership names for every user:

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $member = MS_Factory::load( 'MS_Model_Member', $user_id );
    $memberships = $member->get_membership_ids();
    foreach( $memberships as $key => $membership_id ){ $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
    .
    .
    .
    echo "\n".'window.intercomSettings["Membership"] = "' . $membership->name . '";';

    How can I do the same for the coupon code which they used? For example, one of the codes is "wework".

  • Tho Bui

    Hi Peter

    I hope you're well today!

    I have prepared a custom snippet which you can use as a guide here :
    https://gist.github.com/wpmudev-sls/7a85c7d9353d95156e4ff240d51ecf1f

    You can download zip, unzip it and upload file ms-list-member-coupons.php to your wp-content/mu-plugins folder. If that folder doesn't exist you can simply create it.

    If you are not familiar with mu-plugins you can read about them here:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/
    or you can paste it in your child theme's functions.php file.

    Once file is uploaded, you can use the following shortcode:
    [wpmudev_ms_list_member_coupons] in the page where you like to list member's coupons.

    Kind regards
    Tho Bui

  • Peter

    Thanks, That really helps!

    I also want to send the coupon code in their first invoice to another plugin (intercom). This is what I've used so far to get their membership name:

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $member = MS_Factory::load( 'MS_Model_Member', $user_id );
    $memberships = $member->get_membership_ids();
    foreach( $memberships as $key => $membership_id ){ $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
    .
    .
    .
    echo "\n".'window.intercomSettings["Membership"] = "' . $membership->name . '";';

    How can I replicate this to also get their coupon name for their first invoice?

    Thanks,

    Peter

  • Tho Bui

    Hi Peter

    You can use this custom function to get the coupon code from the first paid invoice:

    /**
             * Get first paid invoice of member by membership
             * @param  init $membership_id membership id
             * @param  init $member_id     member id
             * @return mix html or false
             */
            function wpmudev_ms_get_first_paid_membership_invoice($membership_id, $member_id){
                $args = array(
                    'author' => $member_id,
                    'posts_per_page'=> 1,
                    'order'         => 'ASC',
                    'meta_query'     => array(
                        array(
                            'key'   => 'membership_id',
                            'value' => $membership_id,
                        ),
                        array(
                            'key'       => 'status',
                            'value'     => MS_Model_Invoice::STATUS_PAID,
                            'compare'     => '='
                        )
                    ),
                );
                $invoices = MS_Model_Invoice::get_invoices( $args, true );
                if( ! empty( $invoices ) && is_array( $invoices ) ){
                    return wpmudev_ms_get_coupon_code( $invoices[0] );
                }
                return false;
            }

    Please add this custom function to the MU plugin which I sent you before, and then you can call this function as echo wpmudev_ms_get_first_paid_membership_invoice($membership_id, $member->id) in your custom template to get the coupon code.

    Kind regards,
    Tho Bui

  • Peter

    Could you help me by adding it to this code ( where it says CALL HERE), in the same way like I called the Membership Name for the current_user:

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $member = MS_Factory::load( 'MS_Model_Member', $user_id );
    $memberships = $member->get_membership_ids();
    foreach( $memberships as $key => $membership_id ){ $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
    .
    .
    .
    echo "\n".'window.intercomSettings["Membership"] = "' . $membership->name . '";';
    echo "\n".'window.intercomSettings["Coupon"] = "' . CALL HERE . '";';
  • Tho Bui

    Hi Peter!

    Sorry for delay, you can change that script you shared to:

    $current_user = wp_get_current_user();
    $user_id = $current_user->ID;
    $member = MS_Factory::load( 'MS_Model_Member', $user_id );
    $memberships = $member->get_membership_ids();
    foreach( $memberships as $key => $membership_id ){ $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id );
    .
    .
    .
    $first_coupon = wpmudev_ms_get_first_paid_membership_invoice($membership_id, $user_id)
    echo "\n".'window.intercomSettings["Membership"] = "' . $membership->name . '";';
    if( $first_coupon ){
      echo "\n".'window.intercomSettings["Coupon"] = "' . $first_coupon . '";';
    }

    Hope this helps!

    Kind regards,
    Tho Bui

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.