An open API service indexing awesome lists of open source software.

https://github.com/arraypress/edd-register-export-columns

A library for registering custom export columns in Easy Digital Downloads CSV exports.
https://github.com/arraypress/edd-register-export-columns

Last synced: 12 days ago
JSON representation

A library for registering custom export columns in Easy Digital Downloads CSV exports.

Awesome Lists containing this project

README

          

# EDD Export Columns - Register Custom CSV Export Columns

Add custom columns to Easy Digital Downloads CSV exports with custom data and calculations.

## Installation

```bash
composer require arraypress/edd-register-export-columns
```

## Basic Usage

```php
// Add custom columns to customer exports
edd_register_export_columns( 'customers', [
'lifetime_downloads' => [
'label' => 'Total Downloads',
'callback' => function ( $customer_id ) {
return edd_count_file_downloads_of_customer( $customer_id );
}
],
'last_login' => [
'label' => 'Last Login',
'callback' => function ( $customer_id ) {
$customer = new EDD_Customer( $customer_id );
if ( ! $customer->user_id ) {
return 'N/A';
}

$last_login = get_user_meta( $customer->user_id, 'last_login', true );

return $last_login ? date( 'Y-m-d H:i:s', $last_login ) : 'Never';
}
],
'support_tickets' => [
'label' => 'Support Tickets',
'callback' => function ( $customer_id ) {
$customer = new EDD_Customer( $customer_id );

return get_user_meta( $customer->user_id, 'support_ticket_count', true ) ?: 0;
}
]
] );

// Add custom columns to download exports
edd_register_export_columns( 'downloads', [
'total_reviews' => [
'label' => 'Reviews',
'callback' => function ( $download_id ) {
return get_comments_number( $download_id );
}
],
'average_rating' => [
'label' => 'Avg Rating',
'callback' => function ( $download_id ) {
$reviews = get_comments( [
'post_id' => $download_id,
'meta_key' => 'rating',
'fields' => 'ids'
] );

if ( empty( $reviews ) ) {
return 'N/A';
}

$total_rating = 0;
foreach ( $reviews as $review_id ) {
$total_rating += (int) get_comment_meta( $review_id, 'rating', true );
}

return round( $total_rating / count( $reviews ), 2 );
}
],
'custom_field' => [
'label' => 'Custom Field',
'callback' => function ( $download_id ) {
return get_post_meta( $download_id, 'my_custom_field', true );
}
]
] );
```

## Real Examples

```php
function add_export_columns() {
// Customer export columns
$customer_columns = [];

// Add WooCommerce data if available
if ( class_exists( 'WooCommerce' ) ) {
$customer_columns['woo_orders'] = [
'label' => 'WooCommerce Orders',
'callback' => function ( $customer_id ) {
$customer = new EDD_Customer( $customer_id );

return $customer->user_id ? wc_get_customer_order_count( $customer->user_id ) : 0;
}
];

$customer_columns['woo_total_spent'] = [
'label' => 'WooCommerce Total Spent',
'callback' => function ( $customer_id ) {
$customer = new EDD_Customer( $customer_id );

return $customer->user_id ? wc_get_customer_total_spent( $customer->user_id ) : 0;
}
];
}

// Add subscription data
if ( class_exists( 'EDD_Recurring' ) ) {
$customer_columns['active_subscriptions'] = [
'label' => 'Active Subscriptions',
'callback' => function ( $customer_id ) {
return count( EDD_Recurring()->db->get_subscriptions( [
'customer_id' => $customer_id,
'status' => 'active'
] ) );
}
];
}

// Add affiliate data
if ( function_exists( 'affiliate_wp' ) ) {
$customer_columns['referrals'] = [
'label' => 'Referrals Made',
'callback' => function ( $customer_id ) {
$customer = new EDD_Customer( $customer_id );
$affiliate_id = affwp_get_affiliate_id( $customer->user_id );

return $affiliate_id ? affiliate_wp()->referrals->count( [ 'affiliate_id' => $affiliate_id ] ) : 0;
}
];
}

if ( ! empty( $customer_columns ) ) {
edd_register_export_columns( 'customers', $customer_columns );
}

// Download export columns
$download_columns = [
'featured_image' => [
'label' => 'Featured Image URL',
'callback' => function ( $download_id ) {
return get_the_post_thumbnail_url( $download_id, 'full' ) ?: '';
}
],
'download_files_count' => [
'label' => 'Number of Files',
'callback' => function ( $download_id ) {
$files = edd_get_download_files( $download_id );

return count( $files );
}
],
'total_earnings' => [
'label' => 'Total Earnings',
'callback' => function ( $download_id ) {
return edd_get_download_earnings_stats( $download_id );
}
],
'total_sales' => [
'label' => 'Total Sales',
'callback' => function ( $download_id ) {
return edd_get_download_sales_stats( $download_id );
}
]
];

edd_register_export_columns( 'downloads', $download_columns );
}

add_action( 'init', 'add_export_columns' );
```

## Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `$type` | string | **Yes** | Export type ('customers', 'downloads', 'payments', etc.) |
| `$columns` | array | **Yes** | Array of column configurations |
| `$id_field` | string | No | ID field name (default: 'ID') |
| `$error_callback` | callable | No | Error handling function |

### Column Configuration

| Option | Required | Description |
|--------|----------|-------------|
| `label` | **Yes** | Column header text |
| `callback` | **Yes** | Function that returns column value |

## Export Types

Common EDD export types you can extend:

- `customers` - Customer export data
- `downloads` - Download/product export data
- `payments` - Payment/order export data
- `file_downloads` - File download logs
- `customers_export` - Alternative customer export

## Requirements

- PHP 7.4+
- WordPress 5.0+
- Easy Digital Downloads 3.0+

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the GPL-2.0-or-later License.

## Support

- [Documentation](https://github.com/arraypress/edd-register-export-columns)
- [Issue Tracker](https://github.com/arraypress/edd-register-export-columns/issues)