Jepzen
Jepzen

Reputation: 3162

Show custom field on order in woocommerce

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.

Upvotes: 18

Views: 45365

Answers (3)

Nadeem Khan
Nadeem Khan

Reputation: 3424

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>';
}

Upvotes: 10

Warren G
Warren G

Reputation: 191

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 );

Upvotes: 18

C&#233;sar
C&#233;sar

Reputation: 1638

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 );

Upvotes: 4

Related Questions