WooCommerce对国家的定义如下(为简洁而编辑):
class WC_Countries {
public $countries;
public function __construct() {
global $woocommerce, $states;
$this->countries = apply_filters( 'woocommerce_countries', array(
'AF' => __( 'Afghanistan', 'woocommerce' ),
'AX' => __( 'Åland Islands', 'woocommerce' ),
'AL' => __( 'Albania', 'woocommerce' ),
'DZ' => __( 'Algeria', 'woocommerce' ),
// […]
));
}
}
下订单时,国家代码将写入WordPress wp_postmeta
表,并可在任何可以使用get_post_meta()
函数访问订单id的地方提取:
get_post_meta( $order->id, '_shipping_country', true ),
由于我们只是从数据库中检索两个字符,所以问题是如何将运送国家代码(例如AF
)转换为WC_Countries
类中指定的国家名称?
发布于 2014-08-27 09:54:49
您可以使用WC_Countries
访问WC()->countries
类。因此,要从订单中获取国家名称,必须使用:
WC()->countries->countries[ $order->shipping_country ];
在WooCommerce 3.0+上,您应该使用:
WC()->countries->countries[ $order->get_shipping_country() ];
如果您希望获得状态,则在检查是否存在之前,需要检查是否存在,因为WooCommerce并不包括所有状态,所以这里需要什么:
$states = WC()->countries->get_states( $order->get_shipping_country() );
$state = ! empty( $states[ $order->get_shipping_state() ] ) ? $states[ $order->get_shipping_state() ] : '';
发布于 2016-05-23 10:53:31
若要从州代码中获取州名称,可以使用。
WC()->countries->states[$order->billing_country][$order->billing_state];
发布于 2019-10-31 20:08:41
我正在研究如何获得Woocommerce商店的基本地址,而不是购物车或订单地址,这个线程包含了许多与这个主题相关的Google关键字。我想到了下面的答案,我更新了一下:https://wordpress.stackexchange.com/a/334608/177690
private static function afg_getBaseAddressData() {
return array(
'address' => WC()->countries->get_base_address(),
'address-2' => WC()->countries->get_base_address_2(),
'city' => WC()->countries->get_base_city(),
'zip' => WC()->countries->get_base_postcode(),
'state' => WC()->countries->get_base_state(),
'country' => WC()->countries->countries[WC()->countries->get_base_country()],
'mail' => self::afg_getPublicMail()
);
}
private static function afg_getPublicMail() {
//replace by the way you get an email address
return filter_var(get_option('address-public-mail'), FILTER_VALIDATE_EMAIL);
}
public static function afg_getAddressTemplate() {
$datas = apply_filters( 'afg_shop_base_address', self::afg_getBaseAddressData() );
$html = '';
$html .= '<address>';
foreach ($datas as $key => $data) {
if($data) {
$html .= '<p class="address-line '.$key.'">'.$data.'</p>';
}
}
$html .= '</address>';
return $html;
}
https://stackoverflow.com/questions/25528454
复制相似问题