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



我是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();

相关内容

  • 没有找到相关文章

最新更新