Need help with Membership custom coding

I am using Membership and I have 2 membership levels ... free and premium. I wrote some code that checks to see what level a logged in member is, and I enqueue a different JS file for each. This part works...

$member = MS_Model_Member::get_current_member();
$member_array = $member->subscriptions;
$member_object = $member_array[0];
$membership_id = $member_object->membership_id;
if( is_page( 86 ) && ( $membership_id == '357' ) ) {
wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
} else if( is_page( 86 ) && ( $membership_id == '346' ) ) {
wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
}

For now, the JS files are blank .... the reason I am doing this, is so when a person registers and gets redirected to the thank you page, I am going to execute some code in the JS files that submit their email to Aweber.

I tested with both. I registered under the free membership, and then I checked the source code, and the free version of the JS was there ... When I registered as a premium member, the premium JS wasn't in the source code until I refreshed the thank you page. I am using PayPal, but it is currently going through a sandbox. For some reason, when I finalize the payment in PayPal sandbox mode and push the button to go to the thank you page, it doesn't auto log the user in or something is happening here ... it's strange.

  • Ivan

    Hi mediastead !

    It happens because $membership_id changes after your code for the current user. You should check membership status for subscription in this case. So final code looks like:

    $member = MS_Model_Member::get_current_member();
    $member_array = $member->subscriptions;
    $member_object = $member_array[0];
    $member_object->check_membership_status();
    $membership_id = $member_object->membership_id;
    if( is_page( 86 ) && ( $membership_id == '357' ) ) {
    wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
    } else if( is_page( 86 ) && ( $membership_id == '346' ) ) {
    wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
    }

    Best regards,
    Ivan.

  • mediastead

    Ivan! Thanks for the code tweak ... it's better than the solution I came up with. Basically, since the premium member was logged in, but for some reason the membership id still didn't change until a refresh, I added a redirect from the thank you page, to a thank you page clone, and then executed my enqueues. CLUNKY

    I can roll back to this streamlined version thanks to you.

    I'll test it out, and let you know how it goes.

  • Ivan

    Hi mediastead !
    How do you add your code? Do you use some hook after which this code applies?
    Actually, I tested it on your site. I created {wordpress_folder}/wp-content/mu-plugins/wpmu_test.php file via FTP.
    So, final code looks like that

    <?php
    
    function wpmu_test() {
    		$member = MS_Model_Member::get_current_member();
    		$member_array = $member->subscriptions;
    		$member_object = $member_array[0];
    		$member_object->check_membership_status();
    		$membership_id = $member_object->membership_id;
    		if( is_page( 86 ) && ( $membership_id == '357' ) ) {
    		wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
    		} else if( is_page( 86 ) && ( $membership_id == '346' ) ) {
    		wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
    		}
    }
    add_action('wp_enqueue_scripts', 'wpmu_test');

    Note: you should uncomment the last line on your server for applying this code.

    Please, check it.

    Best regards,
    Ivan.

  • mediastead

    If this helps, I turned on WP_DEBUG and grabbed the error being displayed:

    Notice: Undefined offset: 0 in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/themes/mar-theme-child/inc/hooks.php on line 94

    Notice: Trying to get property of non-object in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/themes/mar-theme-child/inc/hooks.php on line 96

    Notice: Undefined offset: 0 in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/mu-plugins/wpmu_test.php on line 6

    Fatal error: Uncaught Error: Call to a member function check_membership_status() on null in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/mu-plugins/wpmu_test.php:7 Stack trace: #0 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/class-wp-hook.php(298): wpmu_test('') #1 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters(NULL, Array) #2 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #3 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/script-loader.php(1296): do_action('wp_enqueue_scri...') #4 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/class-wp-hook.php(298): wp_enqueue_scripts('') #5 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters(NULL, Array) #6 /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-includes/plugin.php(453): WP_Hook->do_action(Array) #7 /home/97914.cl in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/mu-plugins/wpmu_test.php on line 7

  • mediastead

    Okay ... so, the if statement worked for non-members ...

    Now, I am getting the following lines of code in the WP_DEBUG ... I don't know if it matter, because these errors will disappear after I set WP_DEBUG to false again, but I wanted to ask:

    Notice: Trying to get property of non-object in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/plugins/membership/app/model/class-ms-model-relationship.php on line 2994

    Notice: Trying to get property of non-object in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/plugins/membership/app/model/class-ms-model-relationship.php on line 2998

    Notice: Trying to get property of non-object in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/plugins/membership/app/model/class-ms-model-relationship.php on line 3012

    Notice: Trying to get property of non-object in /home/97914.cloudwaysapps.com/pnmezyvmab/public_html/wp-content/plugins/membership/app/model/class-ms-model-relationship.php on line 3016

    Please advise.

  • Ivan

    Hi mediastead !

    First of all, I added this statement is_page( 86 ) before all calculations in your code. Notices usually don't break sites. In this case, it happens because we don't have an invoice yet but we're changing membership status. I guess it will be better if you'll create custom usermeta after choosing membership by a user and then just check this custom usermeta. Or more easy, because you use only two memberships (one is free and one is paid) - you can delete this statement $member_object->check_membership_status(); and change checking in this way

    if( ( $membership_id == '346' ) ) {
       wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
     } else {
       wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
    }

    Best regards,
    Ivan.

  • mediastead

    Ivan,

    $membership_id is still coming from $member_array = $member->subscriptions, so when a user is not a member yet (logged out or logged in but not a member), $member_array = $member->subscriptions is empty. When this is empty, the code where we are trying to find something in this array that doesn't exist ($member_object = $member_array[0]:wink: breaks the website.

    To fix this, all I did was add a conditional to only execute the rest of the code if $member_array = $member->subscriptions is not empty.

    if(!empty($member_array)) {
    }

    This did the trick, and I was able to keep your original code fix.

    Thanks!

  • mediastead

    Ivan,

    I know I said that this code worked, but I am getting the same issue as before ... it works perfectly with a free membership, but when I sign up a test user to a premium membership, when the button to go back to the "/registration-complete" page from PayPal is clicked, I have to refresh the page in order for the user to be logged in, and for the code to execute.

    Just to clarify, I echoed the array onto the page, so I could see if the member was being logged in after paying, and it returned that they were not logged in until I refreshed the page. I am unable to get the proper membership ID to take action on unless the member is logged in after paying.

    You original suggestion to implement "member_object->check_membership_status();" is currently not giving me the results I need.

    This is the code I am using:

    if ( is_page( 86 ) ) {
    
      $member = MS_Model_Member::get_current_member();
      $member_array = $member->subscriptions;
    
      if(!empty($member_array) ) {
          $member_object = $member_array[0];
          $member_object->check_membership_status();
          $membership_id = $member_object->membership_id;
    
          if( ( $membership_id == '357' ) ) {
           wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
          } else if( ( $membership_id == '346' ) ) {
           wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
          }
    
        }
    
      }

    I tested this when you and I first spoke, and it worked, but I am about to launch this website, so I tested it again, and it is no longer functioning as expected.

    Please let me know if you have any other ideas. Basically, when a premium user registers, and they click the button on PayPal to come back to the website, I need this code to enqueue the JavaScript file without having to refresh the page.

    Thanks!

  • Ivan

    Hi mediastead !

    You can add auto-redirect for this case. Final code will look like

    function mar_child_theme_scripts() {
    if ( is_page( 86 ) ) {
      $member = MS_Model_Member::get_current_member();
      $member_array = $member->subscriptions;
    
      if(!empty($member_array) ) {
          $member_object = $member_array[0];
          $member_object->check_membership_status();
          $membership_id = $member_object->membership_id;
    
          if( ( $membership_id == '357' ) ) {
           wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
          } else if( ( $membership_id == '346' ) ) {
           wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
          } else {
    			global $wp;
    			$current_url = add_query_arg( $_GET, home_url( $wp->request ) );
    			wp_redirect( $current_url );
    			exit;
        }
      }
    }
    add_action( 'wp_enqueue_scripts', 'mar_child_theme_scripts' );

    But it can create recursive redirects if $membership_id will have another value after redirect.

    I found another solution for that

    function mar_child_theme_scripts() {
          if ( is_page( 86 ) ) {
    		$user_id = get_current_user_id();
    		if( !empty( $user_id ) ) {
    			$membership_id = get_user_meta( $user_id, 'temp_membership_id' );
    			if ( ( $membership_id == '357' ) ) {
    				wp_enqueue_script( 'mar-aweber-premium-js', get_stylesheet_directory_uri() . '/js/aweber-premium-submit.js', array('jquery'), '', true );
    			} else if( ( $membership_id == '346' ) ) {
    				wp_enqueue_script( 'mar-aweber-free-js', get_stylesheet_directory_uri() . '/js/aweber-free-submit.js', array('jquery'), '', true );
    			}
    		}
    	}
    
      }
      add_action( 'wp_enqueue_scripts', 'mar_child_theme_scripts' );
    
    function wpmu_membership_include_js( $subscription, $invoice, $view ) {
    	$user_id = get_current_user_id();
    	if ( $user_id ) {
    		$data = $view->data;
    		update_user_meta( $user_id, 'temp_membership_id', $data['membership']->id );
    	}
    }
    add_action( 'ms_view_frontend_payment_after_total_row', 'wpmu_membership_include_js', 10, 3 );

    Abd added this code to your site. Please, check it.

    Best regards,
    Ivan.

  • mediastead

    Ivan,

    I appreciate your reply. It seems that because the thank you page requires a refresh in order to capture the correct membership id that both yours and my code requires some kind of workaround.

    Since my current workaround is doing the job, I think I'll keep it as is. I am basically just hitting the default Membership thank you page, and redirecting to a clone thank you page instantaneously. There is maybe the slightest lag, but not detectable.

    Thank you so much for your efforts!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.