Skip to content

Instantly share code, notes, and snippets.

@danmaby
Last active September 13, 2023 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmaby/78412414884d8c2b044a3156dde0cb86 to your computer and use it in GitHub Desktop.
Save danmaby/78412414884d8c2b044a3156dde0cb86 to your computer and use it in GitHub Desktop.
Export WooCommerce Subscriptions to CSV. Provides a `Export Subscriptions` button in Woo admin menu
<?php
/**
* Plugin Name: Export WooCommerce subscriptions
* Plugin URI: https://www.blue37.com/
* Description: Export WooCommerce subscriptions to a CSV file.
* Version: 0.0.1
* Requires at least: 6.0
* Requires PHP: 8.0
* Author: Blue 37
* Author URI: https://blue37.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: ews
* Domain Path: /ews
*
* @package EWS\export_woo_subscriptions
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
die('Access denied.');
}
/**
* Export WooCommerce subscriptions to a CSV file.
*
* This function checks if the current user has administrative permissions and
* if the 'export_subscriptions' query parameter is set. If both conditions are met,
* it queries all subscriptions using WooCommerce Subscriptions, writes them to a CSV file,
* and sends the file to the browser for download.
*
* @hooked admin_init - 10
*/
function ews_export_subscriptions_to_csv() {
// Check if the user has administrative permissions and if the export query parameter is set
if (current_user_can('manage_options') && isset($_GET['export_subscriptions'])) {
// Set the CSV content type and file name in the headers
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="subscriptions.csv"');
// Open a stream to send the CSV output to the browser
$output = fopen('php://output', 'w');
// Write the header row of the CSV file
fputcsv($output, array('Subscription ID', 'Status', 'Customer Email', 'Customer First Name', 'Customer Last Name', 'Start Date', 'End Date', 'Next Payment Date', 'Total'));
// Query all subscriptions from WooCommerce Subscriptions
$subscriptions = wcs_get_subscriptions(array('subscriptions_per_page' => -1));
// Loop through the subscriptions and write each one to the CSV file
foreach ($subscriptions as $subscription) {
$data = array(
'Subscription ID' => $subscription->get_id(),
'Status' => $subscription->get_status(),
'Customer Email' => $subscription->get_billing_email(),
'Customer First Name' => $subscription->get_billing_first_name(),
'Customer Last Name' => $subscription->get_billing_last_name(),
'Start Date' => $subscription->get_date('start'),
'End Date' => $subscription->get_date('end'),
'Next Payment Date' => $subscription->get_date('next_payment'),
'Total' => $subscription->get_total()
);
fputcsv($output, $data);
}
// Close the output stream
fclose($output);
// Terminate the script to ensure no additional output is sent
exit;
}
}
add_action('admin_init', 'ews_export_subscriptions_to_csv');
/**
* Add a custom link to the WooCommerce menu to export subscriptions.
*/
function ews_add_export_subscriptions_link() {
global $submenu;
$submenu['woocommerce'][] = array(
'Export Subscriptions',
'manage_options',
admin_url('?export_subscriptions=1')
);
}
add_action('admin_menu', 'ews_add_export_subscriptions_link');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment