17

Im working on a webshop and follwoing this tutorial http://wcdocs.woothemes.com/snippets/tutorial-customising-checkout-fields-using-hooks-and-filters/ to add some customes fields to my billing.

// Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
 $fields['billing']['billing_gls_name'] = array(
    'label'     => __('Name for pickup person', 'woocommerce'),
'placeholder'   => _x('Name', 'placeholder', 'woocommerce'),
'required'  => true,
'class'     => array('form-row-wide'),
'clear'     => true
 );

 return $fields;
}

This adds my field. So far so good. So my problem is:

How can I view this new field in the orders view? Details for billing only show the usual billing fields.

3
  • Doesn't it show up in the Custom Fields metabox? It does for my custom field. (NB: maybe you have Custom Fields hidden, you might need to unhide them: youtube.com/watch?v=msUjikwkEAw )
    – webaware
    Dec 9, 2012 at 7:15
  • I do have that interface below but no idea how it worked. Anyway i ended up filling out the shipping address fields and these were shown in the admin part anyway so it all worked out
    – Jepzen
    Dec 12, 2012 at 11:01
  • Take a look at my tutorial on customizing the woocommerce checkout Jul 11, 2015 at 15:56

3 Answers 3

17

The first answer (Cesar) was CLOSE to being correct. In case anyone ever comes across this old post trying to find out the same thing, below is the code needed to insert into your functions.php file after the code given by the original poster, tailored to his/her variables as provided. Note that they use the field name "billing_gls_name" and that this is referenced in our new function as "_billing_gls_name". The extra "_" at the beginning is necessary. This works on Wordpress 3.5.1 running WooCommerce 2.0.3.

function your_custom_field_function_name($order){
    echo "<p><strong>Name of pickup person:</strong> " . $order->order_custom_fields['_billing_gls_name'][0] . "</p>";
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'your_custom_field_function_name', 10, 1 );
9

After defining your custom field (that you did in your code mentioned above), add the code mentioned below to:

  1. Process your field

  2. Save it in the database as Order meta data

  3. Display it in the 'Order details' in the Woocommerce->Orders section

Process your field:

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {

if (!$_POST['billing']['billing_gls_name']) {
    wc_add_notice(__('Please tell us the Location Type that we are billing to.'), 'error');
}

}

Save Field in the DB as Order Meta Data:

add_action('woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta($order_id) {

  if (!empty($_POST['billing']['billing_gls_name'])) {
     update_post_meta($order_id, 'Billing Gls Name', esc_attr($_POST['billing']['billing_gls_name_type']));
     }
 }

And finally display it in the Order details screen:

  add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);

  function my_custom_billing_fields_display_admin_order_meta($order) {
echo '<p><strong>' . __('Billing Gls Name') . ':</strong><br> ' . get_post_meta($order->id, '_billing_gls_name', true) . '</p>';
}
1
  • Thanks for the examples @nk-47. I think you're missing an apostrophe before 'my_custom_checkout_field_update_order_meta' on the DB example.
    – DeFeNdog
    Dec 2, 2014 at 21:16
3

Adding the action woocommerce_admin_order_data_after_billing_address you can insert some data after billing info. Custom fields are under $order->order_custom_fields array.

function display_rfc_in_order_metabox($order){
    echo "<p><strong>RFC:</strong> {$order->order_custom_fields['_billing_rfc'][0]}</p>";
}

add_action( 'woocommerce_admin_order_data_after_billing_address', 'rxm_details_to_order', 10, 1 );

Not the answer you're looking for? Browse other questions tagged or ask your own question.