WP-e-Commerce icon indicating copy to clipboard operation
WP-e-Commerce copied to clipboard

Export csv when using the filter

Open mihaijoldis opened this issue 9 years ago • 3 comments

if you select for example this quarter, it shjows the entries but when you click download you get nada, just the headers

problem looks to be inside wpsc_purchase_log_csv the sql for isset( $_REQUEST['m'] ) condition

mihaijoldis avatar Feb 17 '16 16:02 mihaijoldis

Here is my working code that replaces the current one here: https://github.com/wp-e-commerce/WP-e-Commerce/blob/master/wpsc-admin/init.php#L122-L134

            $where = array( '1 = 1' );

            add_filter( 'date_query_valid_columns', 'set_date_column_to_date' );

            if ( strlen( $_REQUEST['m'] ) < 4 ) {
                $query_args = assemble_predefined_periods_query( $_REQUEST['m'] );
            } else {
                $query_args = array(
                    'year'     => (int) substr( $_REQUEST['m'], 0, 4),
                    'monthnum' => (int) substr( $_REQUEST['m'], -2 ),
                );
            }

            $date_query = new WP_Date_Query( $query_args , $column = '__date__' );
            /* this is a subtle hack since the FROM_UNIXTIME doesn't survive WP_Date_Query
             * so we use __date__ as a proxy
             */
            $where[] = str_replace( '__date__', 'FROM_UNIXTIME(p.date)', $date_query->get_sql() );

            $where = implode( ' ', $where );

            $month_year_sql = apply_filters( 'wpsc_purchase_log_month_year_csv', "
                SELECT * FROM " . WPSC_TABLE_PURCHASE_LOGS . " AS p
                WHERE {$where}
                ORDER BY `id` DESC
            " );

            $data = $wpdb->get_results( $month_year_sql, ARRAY_A );
            $csv_name = _x( 'Purchase Log Filtered.csv', 'exported purchase log csv file name', 'wp-e-commerce' );

The code works BUT the only issue is that add_filter( 'date_query_valid_columns', 'set_date_column_to_date' ); and assemble_predefined_periods_query are 2 functions that i had to copy as is from https://github.com/wp-e-commerce/WP-e-Commerce/blob/master/wpsc-admin/includes/purchase-log-list-table-class.php and have added them at the bottom of the init file.

If there is another way without them i got no idea.

mihaijoldis avatar Feb 17 '16 20:02 mihaijoldis

Also when some checkout fields when doing an export we get an undefined notice from here: https://github.com/wp-e-commerce/WP-e-Commerce/blob/master/wpsc-admin/init.php#L173

This code change will set the value used below the query to an empty string and avoids throwing the undefined index notice

                if ( null == $collected_data ) {
                    $collected_data['value'] = '';
                } else {
                    $collected_data = $collected_data[0];
                }

mihaijoldis avatar Feb 17 '16 20:02 mihaijoldis

I just encountered this issue on a client site. Basically it was timing out trying to export everything rather than the selected month which is what brought the issue to our attention.

My quick fix was also to replace $args['m'] with $_REQUEST['m'] in _wpsc_download_purchase_log_csv(): https://github.com/wp-e-commerce/WP-e-Commerce/blob/master/wpsc-admin/init.php#L121-L123

However, I prefer the idea of passing $args to _wpsc_download_purchase_log_csv( $args ) rather than referencing $_REQUEST within the function.

Tracing it back, a better solution may be to pass the $_REQUEST args earlier in the WPSC_Purchase_Log_Page class.

WPSC_Purchase_Log_Page->download_csv would need to accept $args and pass them through to _wpsc_download_purchase_log_csv(): https://github.com/wp-e-commerce/WP-e-Commerce/blob/master/wpsc-admin/display-sales-logs.php#L733-L735

Then use $this->download_csv( $_REQUEST ) in line 754: https://github.com/wp-e-commerce/WP-e-Commerce/blob/master/wpsc-admin/display-sales-logs.php#L754

I haven't written or tested a patch for this, but that seems logical?

benhuson avatar Oct 26 '17 10:10 benhuson