Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Customizing checkout fields using actions and filters] Add new shipping fields to WooCommerce
// 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['shipping']['shipping_phone'] = array(
'label' => __('Phone', 'woocommerce'),
'placeholder' => _x('Phone', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Phone From Checkout Form').':</strong> ' . get_post_meta( $order->get_id(), '_shipping_phone', true ) . '</p>';
}
@aefurrer
Copy link

aefurrer commented May 17, 2018

As a follow up, here is what I was able to decipher, via this old Forum:
https://wordpress.org/support/topic/added-checkout-fields-not-saving-to-post_meta/

Firstly, it appears the gist on this page only works since the field being added is prefixed with shipping_ .

In my specific situation, I wanted to add a new billing field, which were not saving to DB when I wrote it like this:

$fields['billing']['hotel_name']

But changing it to this, worked for me:

$fields['billing']['billing_hotel_name']

So docs would be well served to be updated to state that the field key must be prefixed appropriately to work.

@alebeta90
Copy link

alebeta90 commented Aug 18, 2018

Hello friends! I have the next code to add the field to specify a domain name

`// 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_domain1'] = array(
'label' => __('Domain', 'woocommerce'),
'placeholder' => _x('example.com', 'placeholder', 'woocommerce'),
'required' => false,
'class' => array('form-row-wide'),
'clear' => true
);

 return $fields;

}

/**

  • Display field value on the order edit page
    */

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '

'.__('Domain Name').': ' . get_post_meta( $order->get_id(), '_billing_domain1', true ) . '

';
}`

In another script im trying to get the information from the fields

$order_billing_first_name = $order_data['billing']['first_name']; $order_billing_last_name = $order_data['billing']['last_name']; $order_billing_company = $order_data['billing']['company']; $order_billing_address_1 = $order_data['billing']['address_1']; $order_billing_city = $order_data['billing']['city']; $order_billing_state = $order_data['billing']['state']; $order_billing_postcode = $order_data['billing']['postcode']; $order_billing_country = $order_data['billing']['country']; $order_billing_email = $order_data['billing']['email']; $order_billing_phone = $order_data['billing']['phone']; $order_billing_domain = $order_data['billing']['domain1'];

All the fields are sending their information properly beside for the domain1. What is wrong with my code? I had been reading in the internet and i can not find the error.

Thanks

@diegoALCE95
Copy link

diegoALCE95 commented Oct 27, 2020

As a follow up, here is what I was able to decipher, via this old Forum:
https://wordpress.org/support/topic/added-checkout-fields-not-saving-to-post_meta/

Firstly, it appears the gist on this page only works since the field being added is prefixed with shipping_ .

In my specific situation, I wanted to add a new billing field, which were not saving to DB when I wrote it like this:

$fields['billing']['hotel_name']

But changing it to this, worked for me:

$fields['billing']['billing_hotel_name']

So docs would be well served to be updated to state that the field key must be prefixed appropriately to work.

Not working for me, not sure what I'm doing wrong, this is my whole code. Some help is appreciated.

 /**
 * Agregar campos al checkout - Add fields to checkout
 */ 

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {
	// Tipo de documento - Document type
     $fields['billing']['billing_tipo_de_documento'] = array(
        'label'     => __('Tipo de documento', 'woocommerce'),
    'placeholder'   => _x('Escoge el tipo de documento', 'placeholder', 'woocommerce'),
    'required'  => true,
    'class'     => array('form-row-left'),
    'clear'     => false,
	'type' 		=> 'select',
	'options'	=> array(
		'option_1' => 'C.C',
		'option_2' => 'C.E',
		'option_3' => 'NIT'
	)
     );
	
	// Número de documento -Document number
	$fields['billing']['billing_numero_de_documento'] = array(
        'label'     => __('Número de documento', 'woocommerce'),
    'placeholder'   => _x('Escribe el número de tu documento', 'placeholder', 'woocommerce'),
	'type' 		=> 'number',
    'required'  => true,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

/**
 * Mostrar valores de los campos en la página de editar orden - Display field value on the order edit page
 */

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
	echo '<p><strong>'.__('Tipo de documento').':</strong> ' . get_post_meta( $order->get_id(), '_tipo_de_documento', true ) . '</p>';
	echo '<p><strong>'.__('Número de documento').':</strong> ' . get_post_meta( $order->get_id(), '_numero_de_documento', true ) . '</p>';
}

It's only showing the text, not the values. Any help is appreciated, thx.

@UVLabs
Copy link

UVLabs commented May 29, 2021

For anyone wondering, you need to actually save the meta value or else you won't see it on the order page. See this answer: https://wordpress.stackexchange.com/a/376228/76433

add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_fields_update_order_meta' );

function custom_checkout_fields_update_order_meta( $order_id ) {
    update_post_meta( $order_id, 'billing_colonia', sanitize_text_field( $_POST['billing_colonia'] ) );
    update_post_meta( $order_id, 'shipping_colonia', sanitize_text_field( $_POST['shipping_colonia'] ) );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment