如何从 WooCommerce 订单中获取完整的州名称?

How to get the complete state name from a WooCommerce order?

我是 WordPress 和 WooCommerce 插件等方面的新手。我正在编写一个插件,我需要将一些数据发送到 SOAP API,情况是 API 只接受州全名,但我只从 WooCommerce 获得缩写形式。

require_once("grutinet-web-service.php");

add_action('woocommerce_thankyou', 'send_order_grutinet');
function send_order_grutinet($order_id)
{

// get order object and order details
$order = new WC_Order($order_id);

// set the address fields
$user_id = $order->get_user_id();
$address_fields = array(
    'country',
    'title',
    'first_name',
    'last_name',
    'company',
    'address_1',
    'address_2',
    'address_3',
    'address_4',
    'city',
    'state',
    'postcode'
);

$address = array();
if (is_array($address_fields)) {
    foreach ($address_fields as $field) {
        $address['billing_' . $field] = get_user_meta($user_id, 'billing_' . $field, true);
        $address['shipping_' . $field] = get_user_meta($user_id, 'shipping_' . $field, true);
    }
}


$numeroPedido   = $order_id;
$tipoPedido     = 'NORMAL';
$nombre         = $address['billing_first_name'] . $address['billing_last_name'];
$DniCif         = '41528741';
$direccion      = $address['shipping_address_1'] . ' - ' . $address['shipping_address_2'];
$codigoPostal   = $address['shipping_postcode'];
$poblacion      = $address['shipping_city'];
$provincia      = $order->get_shipping_state();
// $provincia      = $address['shipping_state'];
[...]

重要的一点是:

$provincia      = $order->get_shipping_state();
// $provincia      = $address['shipping_state'];

如何在此处获取完整的州名称而不是缩写?

您可以使用以下内容显示来自国家和州代码的州名:

$country_code = $order->get_shipping_country();
$state_code   = $order->get_shipping_state();

$countries = new WC_Countries(); // Get an instance of the WC_Countries Object

$country_states = $countries->get_states( $country_code ); // Get the states array from a country code
$state_name     = $country_states[$state_code]; // get the state name

// Display state name (for testing)
echo 'State name: ' . $state_name . '<br>';

已测试并有效。

您还可以使用帐单邮寄国家和州:

$country_code = $order->get_billing_country();
$state_code   = $order->get_billing_state();