Home / Archive / MemberPress: Export Account Type in CSV
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Export Account Type in CSV

Add the following code to add a new CSV export option for Corporate Accounts that includes the account type. Then visit [site-url]/wp-admin/admin.php?export_members_extended to generate and download the CSV.

Code Preview
php
<?php
add_action( 'admin_init', function() {
 	if ( isset( $_GET['export_members_extended'] ) ) {
      $all = MeprUser::list_table(
        /* $order_by */       'user_login',
        /* $order */          'ASC',
        /* $paged */          '',
        /* $search */         '',
        /* $search_field */   'any',
        /* $perpage */        '',
        /* $params */         $_REQUEST,
        /* $include_fields */ true
      );
		
	  $csv_results = array();
	
	  foreach( $all['results'] as $row ) {
	    $user = get_user_by( 'id', $row->ID );
		$caid = get_user_meta( $user->ID, 'mpca_corporate_account_id' );
		$ca_type = __( 'None', 'memberpress-corporate' );
        $user_corporate_accounts = MPCA_Corporate_Account::get_all_by_user_id( $user->ID );
		  
		if( !empty( $caid ) ) {
          $ca_type = __( 'Sub Account', 'memberpress-corporate' );
        }
        elseif( !empty( $user_corporate_accounts ) ) {
          // Check if the member is a corporate account owner
          $ca_type = __( 'Corp Account', 'memberpress-corporate' );
        }
		  
		$row->role = $ca_type;
		  
		$csv_results[] = $row;
	  }
		
	  $filename = 'members-ca-'.time();
      add_filter( 'mepr_process_csv_cell', 'mepr_process_custom_field', 10, 2 );
	  MeprUtils::render_csv( $csv_results, $filename );
 	}
} );
function mepr_process_custom_field( $field, $label ) {
    $mepr_options = MeprOptions::fetch();
    // Pull out our serialized custom field values
    if( is_serialized( $field ) ) {
      $field_settings = $mepr_options->get_custom_field( $label );
      if( empty( $field_settings ) ) {
        return $field;
      }
      if( $field_settings->field_type == 'multiselect' ) {
        $field = unserialize( $field );
        return implode( ',',$field );
      }
      else if( $field_settings->field_type == 'checkboxes' ) {
        $field = unserialize( $field );
        return implode( ',',array_keys( $field ) );
      }
    }
    return $field;
  }

Comments

Add a Comment