I´m currently trying to add a custom field to the default address fields (firstname, lastname, etc...)
The field should be used, to set a salutation for the customer.
For this purpose I used the following filter:
add_filter( 'woocommerce_default_address_fields', 'custom_woocommerce_address_fields' );
function custom_woocommerce_address_fields($fields) {
$fields['salutation'] = array(
'label' => __('Anrede', 'woocommerce'), // Add custom field label
'placeholder' => 'CA12345678', // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'select', // add field type
'options' => array(
'Herr' => 'Herr',
'Frau' => 'Frau',
'Firma' => 'Firma'
),
'priority' => 1, // add priority
'class' => array('billing-salutation-input')// add class name
);
return $fields;
}
Saving the custom field:
add_action( 'woocommerce_checkout_update_order_meta', 'save_new_checkout_field' );
function save_new_checkout_field( $order_id ) {
if ( $_POST['billing_salutation'] ) update_post_meta( $order_id, '_salutation', esc_attr( $_POST['billing_salutation'] ) );
}
Problem:
When I look at the order details, the new field won´t show up under the default address fields.
So I used the following code to show the new data.
add_action( 'woocommerce_admin_order_data_after_billing_address', 'show_new_checkout_field_order', 10, 1 );
function show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_salutation', true ) ) echo '<p><strong>Anrede:</strong> ' . get_post_meta( $order_id, '_salutation', true ) . '</p>';
}
However, the field is always displayed at the bottom and I can't find a way to bring the field to the desired position (image)
Do I have to use a different hook to display the data at the desired location (billing details)?
Or do I have to edit a specifc woocommerce template?