Search code examples
phpwoocommercehook-woocommercecheckoutorders

Display the region name in WooCommerce admin order pages


Hello I'm using Select Region Then City in WooCommerce checkout previous answer to one of my questions, to add checkout fields for region and city dropdown fields.

Select Region Then City in WooCommerce checkout

And I am displaying the selected region in admin order page with the following:

add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
function aba_checkout_field_display_admin_order_meta( $order ){
     $value = $order->get_meta( '_billing_region' );

    if ( ! empty($value) ) {
        echo '<p><strong>'.__('Region').':</strong> ' . $value . '</p>';
    }
}

But it's displaying an array of values and I want to display a text.


Solution

  • I think that you mean that is displaying the selected region slug, but I don't get any array when using my previous answer code.

    Based on my previous answer code, to display the selected region name, you need to replace your code with the following:

    // Utility function: Get the billing region name
    function get_billing_region_name( $order ) {
        $region_slug = $order->get_meta('_billing_region');
        $all_regions = get_regions(); 
        return $region_slug ? $all_regions[$region_slug] : null;
    }
    
    // Display the region in admin edit order pages
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'aba_checkout_field_display_admin_order_meta', 10, 1 );
    function aba_checkout_field_display_admin_order_meta( $order ){
        if ( $region = get_billing_region_name( $order ) ) {
            echo '<p><strong>'.__('Region').':</strong> ' . $region . '</p>';
        }
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.