Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show custom field on order in woocommerce

Tags:

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.

like image 213
Jepzen Avatar asked Oct 18 '12 15:10

Jepzen


People also ask

How do I display custom fields in WooCommerce orders in admin panel?

First, to create a field, go to WooCommerce > Custom Order Fields. Click “Add Field” and begin creating your order field. The “label” is the field name, and will be displayed in the order details. The “description” will be displayed to the user upon hovering over the “?” symbol.

How do I display custom fields on a WooCommerce product page?

Add a custom field value for each product Click the 'Products' link on the left of the WordPress admin. Next, click on the product for which you want to add a custom field value. Scroll down on the 'Edit Product' screen until you see the custom field. Enter a custom field value and Update the product.

How do I add a column in WooCommerce orders page?

When viewing the “Orders” list in WooCommerce, you'll see a set of columns that will show you details about orders in your store. While you can remove columns easily using the “Screen options” tab, adding columns requires some custom code to determine what the column data should be.


3 Answers

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 );
like image 134
Warren G Avatar answered Nov 24 '22 13:11

Warren G


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>';
}
like image 31
Nadeem Khan Avatar answered Nov 24 '22 14:11

Nadeem Khan


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 );
like image 25
César Avatar answered Nov 24 '22 15:11

César