Last active
May 29, 2023 21:47
-
-
Save DavidAnderson684/85a67e50f52d2defc6b13f0d27ede5a0 to your computer and use it in GitHub Desktop.
WooCommerce VAT compliance CSV download alterations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// See: https://www.simbahosting.co.uk/s3/faqs/when-downloading-a-detailed-csv-how-can-i-add-an-extra-column/ | |
// Set the list of desired columns for the spreadsheet | |
add_filter('wc_eu_vat_compliance_csv_header_columns', function() { | |
$desired_columns = [ | |
'Order number', | |
'Date (local)', | |
'Customer Name', | |
'Order status', | |
'Reporting currency', | |
'Products total', // "This is the total price of all the products in the shopping cart prior to shipping cost or VAT cost." | |
'Shipping Total', | |
'VAT rate (%, 1)', | |
'VAT paid (total, reporting currency)', | |
'Sales total (reporting currency)', // " The column “Sales Total” should be the sum of “Products Total” + “Shipping Total” + “VAT Paid”." | |
'Shipping country' | |
]; | |
return $desired_columns; | |
}); | |
// Make sure that the order meta fields needed are fetched from the database when selecting data for the report. | |
add_filter('wc_eu_vat_compliance_report_extra_meta_fields', function($fields, $print_as_csv) { | |
if ($print_as_csv) { | |
$fields[] = '_order_shipping'; | |
$fields[] = '_billing_company'; | |
$fields[] = '_order_total'; | |
} | |
return $fields; | |
}, 10, 2); | |
// Give the order meta fields appropriate internal keys | |
add_filter('wc_eu_vat_compliance_get_report_results_store_key', | |
function($store, $res) { | |
if ('_order_shipping'== $res->meta_key) return 'shipping_amount'; | |
if ('_billing_company' == $res->meta_key) return 'billing_company'; | |
if ('_order_total' == $res->meta_key) return 'order_total'; | |
return $store; | |
}, 10, 2); | |
// Provide the final values for a row | |
add_filter('wc_eu_vat_compliance_csv_data_entries', | |
function($data, $order_id, $order, $status, $results, $anonymised, $original_data, $order_converted) { | |
if (!isset($order['shipping_amount'])) { | |
$shipping_total = 0; | |
$data['Shipping Total'] = ''; | |
} else { | |
$shipping_total = WooCommerce_EU_VAT_Compliance()->round_amount($order_converted['shipping_amount']); | |
$data['Shipping Total'] = $shipping_total; | |
} | |
// This is the WooCommerce meta value for the entire order | |
$order_total = $order_converted['_order_total']; | |
$data['Products total'] = WooCommerce_EU_VAT_Compliance()->round_amount($order_total - $shipping_total - $data['VAT paid (total, reporting currency)']); | |
$data['Customer Name'] = isset($order['billing_company']) ? $order['billing_company'] : ''; | |
return $data; | |
}, 10, 8); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment