When downloading a detailed CSV, how can I add an extra column?

A PHP developer can do this by using the hooks built-in to the plugin. We assume that the desired column has been stored in your database as an ‘order meta’ item. (If it is not, then you will need more complex processing). Below is a code example which takes an order meta item with key “PayPal Transaction Fee” and adds it to a new “PayPal Fee” column in the CSV. A developer can use and customise this example to achieve the desired result. As another example, the exact same code as below but replacing PayPal Transaction Fee with _order_shipping and PayPal Fee with Shipping Amount will add a column for the order shipping amount. A more complicated example that adds new computed columns, removes others, and re-orders them, is available here.

Note: This requires version 1.28.0 or later of the plugin. Earlier versions used a different filter (wc_eu_vat_compliance_report_meta_fields) in the first code fragment which is now deprecated and should no longer be used. If you are updating your code from an earlier version, then please look at the updated example in the first fragment below.

// Make sure that the order meta field 'PayPal Transaction Fee' is fetched from the database when selecting data for the report.
add_filter('wc_eu_vat_compliance_report_extra_meta_fields', 'my_wc_eu_vat_compliance_report_extra_meta_fields', 10, 2);
function my_wc_eu_vat_compliance_report_extra_meta_fields($fields, $print_as_csv) {
if ($print_as_csv) $fields[] = 'PayPal Transaction Fee';
return $fields;
}

// Give the order meta field ‘PayPal Transaction Fee’ the (internal) key ‘pp_fee’
add_filter(‘wc_eu_vat_compliance_get_report_results_store_key’, ‘my_wc_eu_vat_compliance_get_report_results_store_key’, 10, 2);
function my_wc_eu_vat_compliance_get_report_results_store_key($store, $res) {
return (‘PayPal Transaction Fee’ == $res->meta_key) ? ‘pp_fee’ : $store;
}

// Add a column header ‘PayPal Fee’ to the spreadsheet
add_filter(‘wc_eu_vat_compliance_csv_header_columns’, ‘my_wc_eu_vat_compliance_csv_header_columns’);
function my_wc_eu_vat_compliance_csv_header_columns($columns) {
$columns[] = ‘PayPal Fee’;
return $columns;
}

// For the ‘PayPal Fee’ column, insert the value that we fetched earlier (pp_fee)
add_filter(‘wc_eu_vat_compliance_csv_data_entries’, ‘my_wc_eu_vat_compliance_csv_data_entries’, 10, 3);
function my_wc_eu_vat_compliance_csv_data_entries($data, $order_id, $order) {
$data[‘PayPal Fee’] = isset($order[‘pp_fee’]) ? $order[‘pp_fee’] : ”;
return $data;
}

 

Posted in: WooCommerce EU/UK VAT Compliance