-1

How can I get a custom billing field from my WooCommerce order? For example I've WooCommerce Germanized installed which adds a custom billing field named vat_id.

I've found a function get_address_prop() in the WooCommerce orders class but this function is protected so I can't call it with $order->get_address_prop().

Any ideas? Thanks!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mr. Jo
  • 4,946
  • 6
  • 41
  • 100
  • Sorry but for billing custom fields (or shipping custom fields or any custom field), simply use the `WC_Data` method `get_meta()` with the correct meta key on the `$order` object instance or also WordPress `get_post_meta()` with the correct meta key from the order Id… – LoicTheAztec Oct 20 '20 at 23:01
  • For germanized "vat_id" simply use `$vat_id = $order->get_meta('_billing_vat_id');` – LoicTheAztec Oct 20 '20 at 23:44

1 Answers1

0

This is my function I've created to retrieve any custom billing field I want:

/**
 * Returns a custom billing field value
 *
 * @param object $order
 * @param string $custom_field
 *
 * @return string
 */
function get_custom_billing_field( object $order, string $custom_field ): string {
    $order_data       = $order->get_data();
    $custom_field_val = '';

    if ( ! empty( $order_data ) && isset( $order_data['billing'][ $custom_field ] ) ) {
        $custom_field_val = $order_data['billing'][ $custom_field ];
    }

    return apply_filters( 'get_custom_billing_field', $custom_field_val );
}

This is an example call for retrieving the vat_id set in WooCommerce Germanized Pro:

$order = wc_get_order( 22 );

get_custom_billing_field( $order, 'vat_id' );

I hope this helps someone. If you got a better idea, please let me know.

Mr. Jo
  • 4,946
  • 6
  • 41
  • 100