The documentation provided in the link above worked for me! However, switching from the old way of adding custom fields was originally described in this post was a little difficult for me to grasp at first, even with the documentation.
So here’s a summary of what I learned, and what I thought was missing from the documentation:
The old way required three steps to add new fields, the new way only requires one or two, depending on whether or not the custom field value is coming directly from order meta-data (only one step), or if the custom field value is going to be processed further with special programming (two steps).
If you only need to directly add new order meta-data values, then follow the steps at the top of the documentation How to add custom field to export report? and leave the “not in meta” check boxes blank. Also note that the “field name” can be anything and will be what is written in the header of the .csv file. The “field key” should match the name of the meta-data name…
If you need to do some post processing on the meta-data, or just want to write your own custom code to generate data for the field, then in addition to adding the field keys to the admin menu by following the first step above (How to add custom field to export report?) you need to check the “not in meta” boxes, and then add your custom code as the documentation suggests under Adding field other than postmeta value. Make sure the “field key” entered in step one matches exactly the term used in the case statement of the wsoe_add_custom_field_to_export function. There is no need to use any of the old hooks and functions besides the new one (you may delete them if you had them in your functions file).
And if you were adding custom fields the old way, then here’s some additional advice:
Note that the old examples used $od as a variable that was being passed into the add_action(‘wpg_before_csv_write’, ‘csv_write’, 10, 3); example. Now, in the new example, $od is now being represented as $order_details, thus if you had $od in your old code, you must change it if you are copying it into the case statement. You should also check that you don’t return from a case statement without pushing something on to the $csv_values array or you may end up with missing commas in your .csv (comma separated value) file.
Note also that you may need to click “save” numerous times throughout the process in order for the reorder fields to show up correctly…
Also, for new users, I think it would help to see an actual chunk of code that might be inserted in the case statement, so I’m including what I’ve written to add three separate new custom fields here as a complete example:
add_action('wsoe_addon_add_to_csv', 'wsoe_add_custom_field_to_export', 10, 6 );
function wsoe_add_custom_field_to_export( &$csv_values, $order_details, $key, $fields, $item_id, $current_item ) {
switch ( $key ) {
case '_DKGs_custom_postcode':
$order_id = $order_details->id;
$postcode = get_post_meta( $order_id, '_shipping_postcode', true );
if ((trim($postcode) == false)){
$postcode = get_post_meta( $order_id, '_billing_postcode', true );
if ((trim($postcode) == false)){
global $woocommerce;
$postcode = $woocommerce->customer->get_shipping_postcode();
if ((trim($postcode) == false)){
$postcode = $woocommerce->customer->get_postcode();
}
}
}
array_push( $csv_values, $postcode );
break;
case '_DKGs_custom_login_name':
$order_id = $order_details->id;
if( empty( $order_id ) ){
$my_username = "Error: no order_id";
array_push( $csv_values, $my_username );
return '';
}
/* Guests do not have a user_login name so this code is not as helpful as what follows*/
$user_id = get_post_meta( $order_id, '_customer_user', true );
$userobj = get_user_by( 'id', $user_id );
//$display_name = $userobj->display_name;
$display_name = $userobj->user_login;
array_push( $csv_values, trim($display_name) );
break;
case '_DKGs_custom_username':
$order_id = $order_details->id;
if( empty( $order_id ) ){
$my_username = "Error: no order_id";
array_push( $csv_values, $my_username );
return '';
}
$firstname = get_post_meta( $order_id, '_billing_first_name', true );
$lastname = get_post_meta( $order_id, '_billing_last_name', true );
$display_name = "";
if ((trim($firstname) == false) and (trim($lastname) == false)){
$user_id = get_post_meta( $order_id, '_customer_user', true );
$userobj = get_user_by( 'id', $user_id );
$display_name = $userobj->user_login;
}
$my_username = trim( $firstname.' '. $lastname.' '. $display_name );
array_push( $csv_values, $my_username );
break;
default:
break;
}
}
One final note, at the time of writing this, I’ve noticed that there is a confusing typo in one of the listed field keys. The field named: “order shopping” prints out the order shipping price, so I expect it is just a typo that should be changed to say: “order shipping”. I bet Ankit will change that soon though…
Good luck to all, and nice job with this plugin add on Ankit…