WooCommerce WC_Admin_Report CSV Export Not Firing

Question

WC_Admin_Report has a method to allow the Export CSV button to appear within the custom report as so…

    public function get_export_button() {
        $current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( wp_unslash( $_GET['range'] ) ) : '7day';
?>
        <a href="#"
                download="report-<?php echo esc_attr( $current_range ); ?>-<?php echo esc_attr( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ); ?>.csv"
                class="export_csv"
                data-export="chart"
                data-xaxes="<?php esc_attr_e( 'Date', 'woocommerce' ); ?>"
                data-groupby="<?php echo esc_attr( $this->chart_groupby ); ?>"
            ><?php esc_html_e( 'Export CSV', 'woocommerce' ); ?></a>
<?php
        }

All fine there, however it simply doesn’t work.
Some digging around shows that it needs the various WooCommerce JS files to be loaded so that there is a listener on the click event which basically creates the csv and forces the download. However I am unable to get this to work.

Is there any reason why my custom report is not loading in the JS files as default, and when I attempt to enqueue them the event doesn’t fire?

I’m enqueuing the scripts as so

function someFunctionName($hook_suffix) {
        wp_register_script('wc-reports', WC()->plugin_url() . '/assets/js/admin/reports' . $suffix . '.js', array( 'jquery', 'jquery-ui-datepicker' ), WC_VERSION );
        wp_enqueue_script('wc-reports');
        wp_enqueue_script('flot');
        wp_enqueue_script('flot-resize');
        wp_enqueue_script('flot-time');
        wp_enqueue_script('flot-pie');
        wp_enqueue_script('flot-stack');
}
add_action('admin_enqueue_scripts', 'someFunctionName');

In woocommerce/assets/js/admin/reports.js there is the following event listener which I believe should fire

// Export
$( '.export_csv' ).click( function() {
...
in progress 0
, , Rich 2 years 2020-01-28T08:39:09-05:00 1 Answer 74 views 0

Answer ( 1 )

    0
    2021-02-03T21:53:30-05:00

    Hi,

    I saw that this question is one year old, but if it can help someone….
    The listener is fired in woocommerce/assets/js/admin/reports.js, but maybe your datas doesn’t meet the conditions to generate a csv.
    If you didn’t build a “chart” for your custom report but only a table to show your record, replace the value of “data-export” to “table” instead of “chart”.
    Replace
    data-export=”chart”
    by
    data-export=”table”

    Thanks

Leave an answer

Browse
Browse