Home > Blockchain >  Order exporting breaks the CSV
Order exporting breaks the CSV

Time:01-16

So I am trying to export the orders via handle_bulk_actions-edit-shop_order and exporting is working fine. For example: Exporting is working fine when someone is inserting Name as First Name but it brokes the CSV when someone is inserting the value like Mr, First Name same happening with the address section as well, Below is the code I am using.

This is what I have done so far but seems like something is missing that is why it is breaking the csv format

add_filter( 'bulk_actions-edit-shop_order', 'custom_bulk_actions' );
function custom_bulk_actions( $actions ) {
    $actions['export_orders_csv'] = __( 'Export Orders to CSV', 'text_domain' );
    return $actions;
}

add_action( 'handle_bulk_actions-edit-shop_order', 'custom_bulk_action_handler', 10, 3 );
function custom_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
    if ( $doaction !== 'export_orders_csv' ) {
        return $redirect_to;
    }

    // Export orders as CSV code goes here...

    $csv_data = ''; // Generate CSV data string
    
     // Add headings to CSV string
    $csv_data .= 'Order ID, Name, Phone Number, Address, City, Product Name, Product URL, Quanitity, Total' . "\n";

    // Get order details for each order
    foreach ( $post_ids as $post_id ) {
        $order = wc_get_order( $post_id );

        // Add order data to CSV string
        $csv_data .= $order->get_id() . ',';
        $csv_data .= $order->get_billing_first_name() . ',';
        $csv_data .= $order->get_billing_phone() . ',';
        
        // Add billing address to CSV string
        $data  = $order->get_data();
        $billing_address_1  = $data['billing']['address_1'];
        $csv_data .= $billing_address_1 . ',';
        
        $csv_data .= $order->get_billing_city() . ',';
        
        
        // Add product details to CSV string
        $items = $order->get_items();
        $product_names = array();
        $product_urls = array();
        foreach ( $items as $item ) {
            $product = $item->get_product();
            $product_names[] = $product->get_name();
            $product_urls[] = $product->get_permalink();
            $product_quantities['qty'] = $item->get_quantity();
        }
    
        $csv_data .= implode( ',', $product_names ) . ",";
        $csv_data .= implode( ',', $product_urls ) . ",";
        $csv_data .= implode( ',', $product_quantities ) . ",";
        $csv_data .= $order->get_total() . "\n";
    }

    // Send CSV data as download
    header( 'Content-Type: text/csv; charset=utf-8' );
    header( 'Content-Disposition: attachment; filename=orders.csv' );
    echo $csv_data;
    exit;
}

[This is how the export file looks like after exporting]

CodePudding user response:

Fields with commas in the string need to be quoted within a CSV file. Wrap your database fields in quotation marks.

$csv_data .= '"' . $order->get_billing_first_name() . '",';

Alternativly investigate the function fputcsv() which will help with this.

  • Related