Adding Custom Columns To CSV Export

Note: This is part of the developer docs and is considered custom code.
Unfortunately, we cannot provide support for custom code at this time as we do not have the additional resources that would be necessary to provide support for custom code.

If you need assistance with this, please reach out to our list of consultants for further assistance:
https://codeable.io/developers/restrict-content-pro/

There are two steps to adding custom columns to a members or payments export:

  1. Add your column header
  2. Add the column data for each member or payment

Adding Your Column Header

A custom column is added with the following filter: rcp_export_csv_cols_{$export_type}, where $export_type is either "members" (for a memberships export) or "payments" (for a payment export).

Here's an example for adding a new "Referrer" column to the memberships export:

/**
 * Add new export column for "Referrer".
 * 
 * @param array $columns Default column headers.
 * 
 * @return array
 */
function ag_rcp_members_export_referrer_header( $columns ) {
    $columns['referrer'] = __( 'Referrer' );

    return $columns;
}
add_filter( 'rcp_export_csv_cols_members', 'ag_rcp_members_export_referrer_header' );

Adding The Column Value For Each Row

The next step is to add the corresponding value for each member or payment.

Memberships

For memberships, this is done with the   rcp_export_memberships_get_data_row filter. Two parameters are passed in:

  1. $row (array) - An array of key/value pairs for the current membership. 
  2. $membership (RCP_Membership) - RCP_Membership object for the current membership being exported.

Here's an example for adding the referrer metadata for each membership:

/**
 * Add the referrer meta value for each membership.
 * 
 * @param array          $row        Array of data to be exported for the current membership.
 * @param RCP_Membership $membership Membership object.
 * 
 * @return array
 */
function ag_rcp_memberships_export_referrer( $row, $membership ) {
	$row['referrer'] = rcp_get_membership_meta( $membership->get_id(), 'referrer', true );

	// Or you can add a piece of user meta like this:
	// $row['my_field'] = get_user_meta( $membership->get_customer()->get_user_id(), 'my_custom_field', true );

	return $row;
}
add_filter( 'rcp_export_memberships_get_data_row', 'ag_rcp_memberships_export_referrer', 10, 2 );

Payments

For payments, this is done with the  rcp_export_payments_get_data_row filter. Two parameters are passed in:

  1. $row (array) - An array of key/value pairs for the current payment.
  2. $payment (object) - Payment object from the database. The payment ID can be retrieved with: $payment->id

Here's an example for adding the transaction ID for each payment (note: you also need to add the column header, as explained previously):

/**
 * Add the transaction ID value for each payment.
 * 
 * @param array  $row     Array of data to be exported for the current payment.
 * @param object $payment Payment object from the database.
 * 
 * @return array
 */
function ag_rcp_payments_export_transaction_id( $row, $payment ) {
    $row['transaction_id'] = $payment->transaction_id;

    return $row;
}
add_filter( 'rcp_export_payments_get_data_row', 'ag_rcp_payments_export_transaction_id', 10, 2 );
Powered by Zendesk