How to get all the fields with billing_* in woocommerce?

Viewed 288

I am using Woocommerce checkout field editor and created several billing fields with name billing_*. For example billing_street, billing_town, billing_avenue, etc other than billing_address_1 and billing_address_2 which are predefined.

But while calling $order->get_formatted_billing_address(); I am getting only billing_address_1 and billing_address_2.

Is there any way of calling all the value of the fields with the name billing_* in woocommerce.

Here's what I tried:

$order->get_billing_address();

But it didn't work!

Thank you in advance!

Screenshot: Screenshot

This is the output I got: Screeshot 2

At the same time,the other fields are showing in the orders page, but not as billing details. like here not as billing details

1 Answers

You can modify the output of the get_formatted_billing_address method of the WC_Order class via the woocommerce_order_get_formatted_billing_address filter hook (as you can see in Adding newly added fields in billing address and shipping address in order emails woocommerce).

In your case, to add custom fields:

  • _billing_street
  • _billing_town
  • _billing_avenue

you can use the following function:

Feel free to change the sort order of the billing fields (by changing the values of the $data array).

// adds the custom fields to the formatted billing address
add_filter( 'woocommerce_order_get_formatted_billing_address', 'add_custom_field_billing_address', 10, 3 );
function add_custom_field_billing_address( $address, $raw_address, $order ) {

   $countries = new WC_Countries();

   // gets country and state codes
   $billing_country = $order->get_billing_country();
   $billing_state   = $order->get_billing_state();

   // gets the full names of the country and state
   $full_country = ( isset( $countries->countries[ $billing_country ] ) ) ? $countries->countries[ $billing_country ] : $billing_country;
   $full_state   = ( $billing_country && $billing_state && isset( $countries->states[ $billing_country ][ $billing_state ] ) ) ? $countries->states[ $billing_country ][ $billing_state ] : $billing_state;

   $data = array(
      $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
      $order->get_billing_company(),
      $order->get_billing_address_1(),
      $order->get_billing_address_2(),
      $order->get_billing_postcode(),
      $order->get_billing_city(),
      wc_strtoupper( $full_state ),
      $order->get_meta( '_billing_street', true ),
      $order->get_meta( '_billing_town', true ),
      $order->get_meta( '_billing_avenue', true ),
   );

   // removes empty fields from the array
   $data = array_filter( $data );
   // create the billing address using the "<br/>" element as a separator
   $address = implode( '<br/>', $data );

   return $address;

}

The code has been tested and works. Add it to your active theme's functions.php.

So now you can use the $order->get_formatted_billing_address(); method to get the billing address formatted with the custom fields included.

Related