2

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)

enter image description here

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
Kalti
  • 491
  • 8
  • 19

2 Answers2

3

The output is a string, which uses <br/> to separate the order billing address details.

So there are 2 possible views:

  • company_name<br/>Firstname Lastname<br/>street<br/>1<br/>1000 Location
  • Firstname Lastname<br/>street<br/>1<br/>1000 Location

There are several possibilities that you can apply to answer your question, but the easiest is to edit the string via the woocommerce_order_get_formatted_billing_address filter hook.

Note: we are going to check whether there is actually a billing company, and adjust our result based on that.

Note: adjust $order->get_meta( '_your_meta_key' ); to your needs.

So you get:

function str_replace_first( $search, $replace, $subject ) {
    $pos = strpos( $subject, $search );
    if ( $pos !== false ) {
        return substr_replace( $subject, $replace, $pos, strlen( $search ) );
    }
    return $subject;
}
 
/**
 * Filter orders formatterd billing address.
 *
 * @since 3.8.0
 * @param string   $address     Formatted billing address string.
 * @param array    $raw_address Raw billing address.
 * @param WC_Order $order       Order data. @since 3.9.0
 */
function filter_woocommerce_order_get_formatted_billing_address( $address, $raw_address, $order ) { 
    // Get meta
    $value = $order->get_meta( '_your_meta_key' );
    
    // When empty, return 
    if ( empty ( $value ) ) return $address;  
    
    // Get billing company
    $billing_company = $order->get_billing_company();
    
    // Not empty
    if ( ! empty ( $billing_company ) ) {
        $address = str_replace_first( '<br/>', '<br/>' . $value . '<br/>', $address );
    } else {
        $address = $value . '<br/>' . $address;
    }       

    return $address;
}
add_filter( 'woocommerce_order_get_formatted_billing_address', 'filter_woocommerce_order_get_formatted_billing_address', 10, 3 );

Result:

  • company_name<br/>meta_value<br/>Firstname Lastname<br/>street<br/>1<br/>1000 Location
  • meta_value<br/>Firstname Lastname<br/>street<br/>1<br/>1000 Location

Used in this answer: https://stackoverflow.com/a/2606638/11987538

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • 2
    You beat me to it while i was writing the answer, LOL! I like your answer better, i'm going to upvote it. – Ruvee Sep 15 '21 at 15:09
  • 2
    @Ruvee There are indeed several possibilities, only it would be easier if the output was an array, as opposed to a string – 7uc1f3r Sep 15 '21 at 15:10
  • 2
    Yes absolutely! I was almost done writing it when i saw you posted your answer. If i had seen your answer sooner i would have never posted mine, because your answer is more legit. – Ruvee Sep 15 '21 at 15:13
2

It's absolutely feasible! You could append it to the existing billing_address_fields data. You just need to customize your data using the right hooks!

So you used woocommerce_default_address_fields filter hook to append $fields['salutation'] to the default data. On the admin screen, you need to do the same but using different hooks.

Here's what we're going to do:

  1. We're going to use woocommerce_localisation_address_formats filter hook to define a placeholder for our custom data.
  2. Then we'll try to add a replacement for the placeholder using woocommerce_formatted_address_replacements filter hook.
  3. And finally we're going to get our custom data and populate the place holder we defined using woocommerce_order_formatted_billing_address filter hook.

1- woocommerce_localisation_address_formats

add_filter('woocommerce_localisation_address_formats', 'admin_customized_localisation_address_formats', 99);

function admin_customized_localisation_address_formats($billing_address_formats)
{
  if (is_admin()) {
    foreach($billing_address_formats as $key => $value) {
      $billing_address_formats[$key] .= "\n{salutation}";
    }
  }
  return $billing_address_formats;
}

2- woocommerce_formatted_address_replacements

add_filter('woocommerce_formatted_address_replacements', 'adding_customized_replacements', 10, 2);

function adding_customized_replacements($replacements, $args)
{
  $replacements["{salutation}"] = !empty($args["salutation"])  ? $args["salutation"] : "";
  return $replacements;
}

3- woocommerce_order_formatted_billing_address

add_filter('woocommerce_order_formatted_billing_address', 'display_customized_order_address_fields_on_admin', 10, 2);

function display_customized_order_address_fields_on_admin($address, $order)
{
  $order_id = $order->get_id();

  $address["salutation"] = get_post_meta($order_id, '_salutation', true) ? 'Anrede: ' . get_post_meta($order_id, '_salutation', true) : "No salutation data";

  return $address;
}

This will output this:

enter image description here

Ruvee
  • 8,611
  • 4
  • 18
  • 44