How to customize Order Number in Paid Membership Pro

If you use PaidMembership Pro and you’re looking to modify the Random Generated Order Code, here is below a simple snippet to do Just that. You can modify the “WUP” into whatever initial you want it to be. 

The code is set to generate your desired Initial (like website, plugins, department initial etc…), the user ID generating that code or making that order, the last 2 digits years and a randomized value to keep things unique. You want to use the plugin “Code Snippets” to add this to your existing site.

 

				
					// we want to modify the code generated by Paid Membership Pro for users Orders
add_filter('pmpro_random_code', 'walup_code_generator', 10, 2);
function walup_code_generator($code) {
  
  			global $wpdb;
			global $current_user;
			$user_id = $current_user->ID;
			$date_year = intval( date_i18n( 'y') );

			// We mix this with the seed to make sure we get unique codes.
			static $count = 0;
			$count++;
			$scramble = 'WUP-'.$user_id.'-'.$date_year.'-'.md5( AUTH_KEY . microtime() . SECURE_AUTH_KEY . $count );
			$code = substr( $scramble, 0, 18 );
			$check = $wpdb->get_var( "SELECT id FROM $wpdb->pmpro_membership_orders WHERE code = '$code' LIMIT 1" );
				if( $check || is_numeric( $code ) ) {
					$code = NULL;
				
			}
    return strtoupper( $code );
} // function walup_code_generator