Inspired by Woocommerce editable custom checkout field and in get_formatted_shipping_address answer code, I have been able to add 2 new billing and shipping fields, with the following code:
// Checkout and my account (add) unit number and floor number
add_filter( 'woocommerce_billing_fields' , 'custom_field_billing_extra' );
function custom_field_billing_extra( $fields ) {
$fields['billing_house_number'] = array(
'type' => 'text', // chose the field type
//'label' => __('Custom label (billing)', 'woocommerce' ),
'class' => array('form-row-first my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
'required' => false, // Optional
'clear' => true, // Optional
'priority' => 61, // To change the field location increase or decrease this value
'placeholder' => __('Unit number', 'custom-domain'),
);
/////////////////////////////
$fields['billing_floor_number'] = array(
'type' => 'text', // chose the field type
//'label' => __('Custom label (billing)', 'woocommerce' ),
'class' => array('form-row-last my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
'required' => false, // Optional
'clear' => true, // Optional
'priority' => 61, // To change the field location increase or decrease this value
'placeholder' => __('Floor number', 'custom-domain'),
);
return $fields;
}
add_filter( 'woocommerce_shipping_fields' , 'custom_field_shipping_extra' );
function custom_field_shipping_extra( $fields ) {
$fields['shipping_house_number'] = array(
'type' => 'text', // chose the field type
//'label' => __('Custom label (billing)', 'woocommerce' ),
'class' => array('form-row-first my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
'required' => false, // Optional
'clear' => true, // Optional
'priority' => 61, // To change the field location increase or decrease this value
'placeholder' => __('Unit number', 'custom-domain'),
);
/////////////////////////////
$fields['shipping_floor_number'] = array(
'type' => 'text', // chose the field type
//'label' => __('Custom label (billing)', 'woocommerce' ),
'class' => array('form-row-last my-custom-class'), // can be also 'form-row-first' or 'form-row-last'
'required' => false, // Optional
'clear' => true, // Optional
'priority' => 61, // To change the field location increase or decrease this value
'placeholder' => __('Floor number', 'custom-domain'),
);
return $fields;
}
// Admin editable single orders shipping and billing new fields
add_filter( 'woocommerce_admin_billing_fields' , 'admin_order_page_new_fields' );
add_filter( 'woocommerce_admin_shipping_fields' , 'admin_order_page_new_fields' );
function admin_order_page_new_fields( $fields ) {
// Include shipping phone as editable field
$fields['house_number'] = array( 'label' => __('House Number', 'custom-domain'), 'show' => '0' );
$fields['floor_number'] = array( 'label' => __('floor Number', 'custom-domain'), 'show' => '0' );
return $fields;
}
// Admin editable User shipping new fields
add_filter( 'woocommerce_customer_meta_fields', 'user_new_field_account' );
function user_new_field_account( $fields ) {
$fields['billing']['fields']['house_number'][type] = $fields['shipping']['fields']['house_number'][type] ='text';
$fields['billing']['fields']['house_number'][label] = $fields['shipping']['fields']['house_number'][label] = __('House Number', 'custom-domain');
$fields['billing']['fields']['house_number'][description] = $fields['shipping']['fields']['house_number'][description] = 'House Number';
$fields['billing']['fields']['floor_number'][type] = $fields['shipping']['fields']['floor_number'][type] ='text';
$fields['billing']['fields']['floor_number'][label] = $fields['shipping']['fields']['floor_number'][label] = __('Floor Number', 'custom-domain');
$fields['billing']['fields']['floor_number'][description] = $fields['shipping']['fields']['floor_number'][description] = 'Floor Number';
return $fields;
}
// add the custom fields to email
add_action('woocommerce_email_customer_details','add_house_number_field_to_emails_notifications', 19, 4 );
function add_house_number_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$output = '';
$shipping_house_number = get_post_meta( $order->id, '_shipping_house_number', true );
if ( !empty($shipping_house_number) )
$output .= '<div><strong>' . __('House Number:', 'custom-domain') . '</strong> <span class="text">' . $shipping_house_number . '</span></div>';
echo $output;
}
add_action('woocommerce_email_customer_details','add_floor_number_checkout_field_to_emails_notifications', 20, 4 );
function add_floor_number_checkout_field_to_emails_notifications( $order, $sent_to_admin, $plain_text, $email ) {
$output = '';
$shipping_floor_number = get_post_meta( $order->id, '_shipping_floor_number', true );
if ( !empty($shipping_floor_number) )
$output .= '<div><strong>' . __('Floor Number:', 'custom-domain') . '</strong> <span class="text">' . $shipping_floor_number . '</span></div>';
echo $output;
}
This is working great but when I create a manual order from the backend the values do not get pull from the user data they are always empty.
what am I missing?