Adjust WooCommerce Memberships Exports

WooCommerce memberships

A popular feature with WooCommerce Memberships is the ability to import and export members. While the default export includes membership data, you may want to include other data here as well. Modifying the export is possible, as there are hooks available to change the CSV output; we’ll go over a couple of straightforward examples in this guide.

Please note this guide requires beginner PHP development and WordPress skills.

Filter WooCommerce Memberships Export Column Headers

The first step in adjusting the member export is the ability to filter the column headers. This makes it possible to add, remove, or re-order columns in the export. This hook is available to modify headers:

apply_filters( 'wc_memberships_csv_export_user_memberships_headers', $headers, $export_instance );

Example 1: Remove unneeded headers

Let’s say you want to remove the membership plan ID and “has_access” columns. Doing so is simple, as you just need to remove the headers for these columns, and they won’t be output.

function sv_wc_memberships_modify_member_export_headers( $headers ) {

    // remove any unwanted headers
    unset( $headers['membership_plan_id'], $headers['has_access'] );
    return $headers;
}
add_filter( 'wc_memberships_csv_export_user_memberships_headers', 'sv_wc_memberships_modify_member_export_headers' );

That’s all it takes!

Example 2: Adding new headers

Adding headers is straight-forward as well:

function sv_wc_memberships_modify_member_export_headers( $headers ) {

    $headers['member_phone'] = 'member_phone';
    return $headers;
}
add_filter( 'wc_memberships_csv_export_user_memberships_headers', 'sv_wc_memberships_modify_member_export_headers' );

However, if you’re going to add a new header, you must also populate it with content.

Filter WooCommerce Memberships Export Column Content

If you want to add new columns to the file, you must also filter the column cell to populate it with the data for each member. We can do so with this hook:

apply_filters( "wc_memberships_csv_export_user_memberships_{$column_name}_column", '', $column_name, $user_membership, $export_instance );

This lets you adjust the data for that column. Notice that the filter name is dynamic — the $column_name will be replaced with the header name you’ve added. The $user_membership parameter is included and will let you get the user data you need.

For example, the user ID can be obtained with: $user_membership->get_user_id(), which can let you get the WP User data.

Let’s take our member_phone example above — we can now populate this cell using the wc_memberships_csv_export_user_memberships_member_phone_column filter (I’ve replaced the column name variable with the key I previously set).

function sv_wc_memberships_modify_member_export_columns( $data, $_, $user_membership ) {
    // return the data for this column
    return get_user_meta( $user_membership->get_user_id(), 'billing_phone', true );
}
add_filter( 'wc_memberships_csv_export_user_memberships_member_phone_column', 'sv_wc_memberships_modify_member_export_columns', 10, 3 );

Notice that I don’t have to scope this data to a particular column, as the filter name does that for me, so I can just return the data I need for the new column.

Adjust WooCommerce Memberships Exports: Putting it Together

Adding columns and removing them is pretty simple, so let’s put these together with a snippet that (1) removes some unwanted columns, (2) adds a new column in a specific position, and (3) adds the data for the new column.

My export will start off like this:

WooCommerce Memberships Export: original data

We’ll remove a couple of columns, and insert the column for the member’s phone number (view snippet here):

/**
 * Modify the member CSV Export column headers.
 *
 * @param string[] $headers array of column headers as 'key' => 'output_name'
 * @return string[] updated headers
 */
function sv_wc_memberships_modify_member_export_headers( $headers ) {

    // remove any unwanted headers
    unset( $headers['membership_plan_id'], $headers['has_access'] );

    $new_headers = array();

    // add a column header for "member phone"
    foreach ( $headers as $key => $name ) {

        $new_headers[ $key ] = $name;

        // add our new header after the member email
        if ( 'member_email' == $key ) {
            $new_headers['member_phone'] = 'member_phone';
        }
    }
    return $new_headers;
}
add_filter( 'wc_memberships_csv_export_user_memberships_headers', 'sv_wc_memberships_modify_member_export_headers' );

/**
 * Adds data for our new member export column.
 *
 * Note that no column name check is needed since the filter name is scoped to the column key.
 *
 * @param string[] $data export data as 'column' => 'data'
 * @param string $_ unused, the column key
 * @param \WC_Memberships_User_Membership $user_membership User Membership object
 * @return string[] updated data
 */
function sv_wc_memberships_modify_member_export_columns( $data, $_, $user_membership ) {
    // return the data for this column
    return get_user_meta( $user_membership->get_user_id(), 'billing_phone', true );
}
add_filter( 'wc_memberships_csv_export_user_memberships_member_phone_column', 'sv_wc_memberships_modify_member_export_columns', 10, 3 );

Now some columns have been removed, and our new column is added after the “member email” column:

WooCommerce Memberships Export: modified data

This can allow you to add further user data, membership data, or plan data to member export files.