Title: Bugged CSV Sales Export : fixed function
Last modified: August 20, 2016

---

# Bugged CSV Sales Export : fixed function

 *  [Vincent Robic](https://wordpress.org/support/users/vrobic/)
 * (@vrobic)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/bugged-csv-sales-export-fixed-function/)
 * Hi,
 * The CSV sales export is not working in WP E-Commerce 3.8.9.5, headers are shifted.
   I fixed the `wpsc_purchase_log_csv()` function, in `wpsc-admin/init.php` :
 * > function wpsc_purchase_log_csv() {
   >  global $wpdb, $wpsc_gateways; get_currentuserinfo();
   > $count = 0; if ( ‘key’ == $_REQUEST[‘rss_key’] && current_user_can( ‘manage_options’)){
   > if ( isset( $_REQUEST[‘start_timestamp’] ) && isset( $_REQUEST[‘end_timestamp’])){
   > $start_timestamp = $_REQUEST[‘start_timestamp’]; $end_timestamp = $_REQUEST[‘
   > end_timestamp’]; $start_end_sql = “SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS."`
   > WHERE `date` BETWEEN ‘%d’ AND ‘%d’ ORDER BY `date` DESC”; $start_end_sql = 
   > apply_filters( ‘wpsc_purchase_log_start_end_csv’, $start_end_sql ); $data =
   > $wpdb->get_results( $wpdb->prepare( $start_end_sql, $start_timestamp, $end_timestamp),
   > ARRAY_A ); /* translators: %1$s is “start” date, %2$s is “to” date */ $csv_name
   > = _x( ‘Purchase Log %1$s to %2$s.csv’, ‘exported purchase log csv file name’,‘
   > wpsc’ ); $csv_name = sprintf( $csv_name, date( “M-d-Y”, $start_timestamp ),
   > date( “M-d-Y”, $end_timestamp ) ); } elseif ( isset( $_REQUEST[‘m’] ) ) { $
   > year = (int) substr( $_REQUEST[‘m’], 0, 4); $month = (int) substr( $_REQUEST[‘
   > m’], -2 ); $month_year_sql = “ SELECT * FROM ” . WPSC_TABLE_PURCHASE_LOGS .“
   > WHERE YEAR(FROM_UNIXTIME(date)) = %d AND MONTH(FROM_UNIXTIME(date)) = %d ORDER
   > BY `id` DESC “; $month_year_sql = apply_filters( ‘wpsc_purchase_log_month_year_csv’,
   > $month_year_sql ); $data = $wpdb->get_results( $wpdb->prepare( $month_year_sql,
   > $year, $month ), ARRAY_A ); /* translators: %1$s is month, %2$s is year */ 
   > $csv_name = _x( ‘Purchase Log %1$s/%2$s.csv’, ‘exported purchase log csv file
   > name’, ‘wpsc’ ); $csv_name = sprintf( $csv_name, $month, $year ); } else { 
   > $sql = apply_filters( ‘wpsc_purchase_log_month_year_csv’, “SELECT * FROM ” .
   > WPSC_TABLE_PURCHASE_LOGS . ” ORDER BY `id` DESC” ); $data = $wpdb->get_results(
   > $sql, ARRAY_A ); $csv_name = _x( “All Purchase Logs.csv”, ‘exported purchase
   > log csv file name’, ‘wpsc’ ); }
   >  $form_sql = “SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active`
   > = ‘1’ AND `type` != ‘heading’ ORDER BY `checkout_order` DESC;”;
   >  $form_data
   > = $wpdb->get_results( $form_sql, ARRAY_A );
   >  $headers_array = array(
   >  _x( ‘Purchase ID’, ‘purchase log csv headers’, ‘wpsc’),
   > _x( ‘Purchase Total’, ‘purchase log csv headers’, ‘wpsc’ ), ); $headers2_array
   > = array( _x( ‘Payment Gateway’, ‘purchase log csv headers’, ‘wpsc’ ), _x( ‘
   > Payment Status’, ‘purchase log csv headers’, ‘wpsc’ ), _x( ‘Purchase Date’,‘
   > purchase log csv headers’, ‘wpsc’ ), ); $output = ”;
   >  foreach ( (array)$data as $purchase ) {
   >  $form_headers = ”; $output .= “\””.
   > $purchase[‘id’] . “\”,”; //Purchase ID $output .= “\”” . $purchase[‘totalprice’].“\”,”;//
   > Purchase Total
   >  $form_headers_array = array();
   >  foreach ( (array)$form_data as $form_field ) {
   >  $form_headers_array[] = $form_field[‘
   > unique_name’]; $collected_data_sql = “SELECT * FROM `" . WPSC_TABLE_SUBMITED_FORM_DATA."`
   > WHERE `log_id` = ‘” . $purchase[‘id’] . “‘ AND `form_id` = ‘” . $form_field[‘
   > id’] . “‘ LIMIT 1”; $collected_data = $wpdb->get_results( $collected_data_sql,
   > ARRAY_A ); $collected_data = $collected_data[0]; $output .= “\”” . $collected_data[‘
   > value’] . “\”,”; // get form fields }
   >  if ( isset( $wpsc_gateways[$purchase[‘gateway’]] ) && isset( $wpsc_gateways[
   > $purchase[‘gateway’]][‘display_name’] ) )
   >  $output .= “\”” . $wpsc_gateways[
   > $purchase[‘gateway’]][‘display_name’] . “\”,”; //get gateway name else $output.
   > = “\”\”,”;
   >  $status_name = wpsc_find_purchlog_status_name( $purchase[‘processed’] );
   >  $output .= “\”” . $status_name . “\”,”; //get purchase status
   >  $output .= “\””.
   > date( “jS M Y”, $purchase[‘date’] ) . “\”,”; //date
   >  $cartsql = “SELECT `prodid`, `quantity`, `name` FROM `" . WPSC_TABLE_CART_CONTENTS."`
   > WHERE `purchaseid`=” . $purchase[‘id’] . “”;
   >  $cart = $wpdb->get_results( $
   > cartsql, ARRAY_A );
   >  if( $count < count( $cart ) )
   >  $count = count( $cart ); // Go through all 
   > products in cart and display quantity and sku foreach ( (array)$cart as $item){
   > $skuvalue = get_product_meta( $item[‘prodid’], ‘sku’, true ); if( empty( $skuvalue))
   > $skuvalue = __( ‘N/A’, ‘wpsc’ ); $output .= “\”” . $item[‘quantity’] . “\”,”;
   > $output .= “\”” . str_replace( ‘”‘, ‘\”‘, $item[‘name’] ) . “\””; $output .
   > = “,” . $skuvalue.”,” ; } $output .= “\n”; // terminates the row/line in the
   > CSV file } // Get the most number of products and create a header for them 
   > $headers3 = “”; for( $i = 0; $i < $count; $i++ ){ $headers3_array[] = _x( ‘
   > Quantity’, ‘purchase log csv headers’, ‘wpsc’ ); $headers3_array[] = _x( ‘Product
   > Name’, ‘purchase log csv headers’, ‘wpsc’ ); $headers3_array[] = _x( ‘SKU’,‘
   > purchase log csv headers’, ‘wpsc’ ); }
   >  $headers = ‘”‘ . implode( ‘”,”‘, $headers_array ) . ‘”‘; // Purchase ID, Purchase
   > total
   >  $form_headers = ‘”‘ . implode( ‘”,”‘, $form_headers_array ) . ‘”‘; //
   > Shipping and billing data $headers2 = ‘”‘ . implode( ‘”,”‘, $headers2_array).‘”‘;//
   > Payment $headers3 = ‘”‘ . implode( ‘”,”‘, $headers3_array ) . ‘”‘; // Items
   >  $headers = apply_filters( ‘wpsc_purchase_log_csv_headers’, $headers . ‘,’ .
   > $form_headers . ‘,’ . $headers2 . ‘,’ . $headers3, $data, $form_data );
   >  $output
   > = apply_filters( ‘wpsc_purchase_log_csv_output’, $output, $data, $form_data);
   > header( ‘Content-Type: text/csv’ ); header( ‘Content-Disposition: inline; filename
   > =”‘ . $csv_name . ‘”‘ ); echo $headers . “\n”. $output; exit; } }
 * You might include this fix in the next release. Use a diff tool to see modified
   lines, or just paste it as is.
 * Vince
 * [http://wordpress.org/extend/plugins/wp-e-commerce/](http://wordpress.org/extend/plugins/wp-e-commerce/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [Justin Sainton](https://wordpress.org/support/users/justinsainton/)
 * (@justinsainton)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/bugged-csv-sales-export-fixed-function/#post-3556952)
 * Hi Vince,
 * Patches are always welcome on GitHub, thanks!
 * [https://github.com/wp-e-commerce/WP-e-Commerce](https://github.com/wp-e-commerce/WP-e-Commerce)

Viewing 1 replies (of 1 total)

The topic ‘Bugged CSV Sales Export : fixed function’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/wp-e-commerce_ebebeb.svg)
 * [WP eCommerce](https://wordpress.org/plugins/wp-e-commerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-e-commerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-e-commerce/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-e-commerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-e-commerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-e-commerce/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Justin Sainton](https://wordpress.org/support/users/justinsainton/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/bugged-csv-sales-export-fixed-function/#post-3556952)
 * Status: not a support question