Skip to main content
Easy Digital Downloads Documentation
Documentation, Reference Materials, and Tutorials for Easy Digital Downloads

EDD Export Class

Overview

The Easy Digital Downloads Export system provides a robust framework for creating custom data exports. This documentation covers the base Exporter class and how to implement custom exporters.

Requirements

  • WordPress 6.2+
  • Easy Digital Downloads 3.3.8+
  • PHP 7.4+

Base Export Class

The Exporter class is located in the EDD\Admin\Exports\Exporters namespace and serves as the foundation for all EDD export functionality.

Required Methods

When extending the Exporter class, you must implement these abstract methods:

abstract protected function get_export_type(): string;
abstract protected function get_data(): array;
abstract protected function get_data_headers(): array;
abstract protected function get_total(): int;
abstract public function set_properties( $request ): void;

Creating a Custom Exporter

1. Basic Structure

Create a class that extends the Exporter class:

<?php

/**
 * Custom Exporter class.
 */
class CustomExporter extends EDD\Admin\Exports\Exporters\Exporter {

	/**
	 * Get the export type.
	 *
	 * @return string The export type.
	 */
	protected function get_export_type(): string {
		return 'custom_export';
	}

	/**
	 * Set the properties.
	 *
	 * @param array $request The request.
	 * @return void
	 */
	public function set_properties( $request ): void {
		$this->start = isset( $request['custom-export-start'] )
			? sanitize_text_field( $request['custom-export-start'] )
			: '';
		$this->end   = isset( $request['custom-export-end'] )
			? sanitize_text_field( $request['custom-export-end'] )
			: '';
	}

	/**
	 * Get the data headers.
	 *
	 * @return array The data headers.
	 */
	protected function get_data_headers(): array {
		return array(
			'id'     => __( 'ID', 'your-text-domain' ),
			'status' => __( 'Status', 'your-text-domain' ),
		);
	}

	protected function get_data(): array {
		$data = array();
		$args = array_merge(
			array(
				'number' => $this->per_step,
				'offset' => ( $this->step * $this->per_step ) - $this->per_step,
			),
			$this->get_base_args()
		);

		// Your data retrieval logic here
		$items = edd_get_orders( $args );

		foreach ( $items as $item ) {
			$data[] = array(
				'id'     => $item->id,
				'status' => $item->status,
			);
		}

		return $data;
	}

	/**
	 * Get the total.
	 *
	 * @return int The total.
	 */
	protected function get_total(): int {
		return edd_count_orders( $this->get_base_args() );
	}

	/**
	 * Get the base args.
	 *
	 * @return array The base args.
	 */
	private function get_base_args(): array {
		$args = array();
		if ( ! empty( $this->start ) || ! empty( $this->end ) ) {
			$args['date_query'] = $this->get_date_query();
		}
		return $args;
	}
}

2. Registering Your Exporter

Create a class to handle the registration and form display:

/**
 * Register the custom exporter.
 *
 * @param \EDD\Admin\Exports\Registry $registry The registry instance.
 * @return void
 */
function custom_register_export( \EDD\Admin\Exports\Registry $registry ) {
	$registry->register_exporter(
		'custom_export',
		array(
			'label'       => __( 'Custom Export', 'your-text-domain' ),
			'description' => __( 'Export custom data.', 'your-text-domain' ),
			'class'       => CustomExporter::class,
			'class_path'  => __DIR__ . '/class-custom-export.php', // Change to your export class path.
			'button'      => __( 'Export Data', 'your-text-domain' ),
		)
	);
}
add_action( 'edd_export_init', 'custom_register_export' );

/**
 * Display the custom export form.
 *
 * @param string $exporter_id The exporter ID.
 * @return void
 */
function custom_export_form( $exporter_id ) {
	if ( 'custom_export' !== $exporter_id ) {
		return;
	}

	$from_to = new \EDD\HTML\FromTo(
		array(
			'legend' => __( 'Custom Export Date', 'your-text-domain' ),
			'id'     => 'order-export',
		)
	);
	$from_to->output();
	?>
		<label for="edd_export_custom_status" class="screen-reader-text"><?php esc_html_e( 'Select Status', 'your-text-domain' ); ?></label>
		<?php
		$statuses = array();
		foreach ( edd_get_payment_statuses() as $status ) {
			if ( 'publish' === $status ) {
				continue;
			}
			$statuses[ $status ] = edd_get_payment_status_label( $status );
		}
		$select = new \EDD\HTML\Select(
			array(
				'id'               => 'edd_export_custom_status',
				'name'             => 'status',
				'show_option_all'  => __( 'All Statuses', 'your-text-domain' ),
				'show_option_none' => false,
				'selected'         => false,
				'options'          => $statuses,
			)
		);
		$select->output();
}
add_action( 'edd_export_form', 'custom_export_form' );

With the new export registry, once you have your export class set up along with your form, EDD will handle the export process automatically.

Sources: