答案 0 :(得分:5)
答案 1 :(得分:1)
只需通过hook覆盖该类,
function woo_override_checkout_fields_billing( $fields ) {
$fields['billing']['billing_country'] = array(
'type' => 'select',
'label' => __('My New Country List', 'woocommerce'),
'options' => array('AU' => 'Australia')
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields_billing' );
function woo_override_checkout_fields_shipping( $fields ) {
$fields['shipping']['shipping_country'] = array(
'type' => 'select',
'label' => __('My New Country List', 'woocommerce'),
'options' => array('AU' => 'Australia')
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'woo_override_checkout_fields_shipping' );
这将有助于您在下拉列表中仅显示1个国家/地区。将此代码添加到theme.php中的functions.php中
答案 2 :(得分:1)
也许您想销售到多个国家,但也只想显示用户连接的国家(通过IP地址进行地理位置分配)。因此,以这种方式,法国用户只会在该国家/地区下拉列表中看到法国,澳大利亚用户只会在该国家/地区下拉列表中看到澳大利亚,依此类推……代码如下:
/**
* @param array $countries
* @return array
*/
function custom_update_allowed_countries( $countries ) {
// Only on frontend
if( is_admin() ) return $countries;
if( class_exists( 'WC_Geolocation' ) ) {
$location = WC_Geolocation::geolocate_ip();
if ( isset( $location['country'] ) ) {
$countryCode = $location['country'];
} else {
// If there is no country, then return allowed countries
return $countries;
}
} else {
// If you can't geolocate user country by IP, then return allowed countries
return $countries;
}
// If everything went ok then I filter user country in the allowed countries array
$user_country_code_array = array( $countryCode );
$intersect_countries = array_intersect_key( $countries, array_flip( $user_country_code_array ) );
return $intersect_countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'custom_update_allowed_countries', 30, 1 );