【发布时间】:2021-04-30 13:43:41
【问题描述】:
我目前使用以下代码设置默认国家/地区(基于用户当前的地理位置)。
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 );
上述代码不仅影响计费部分,还影响运输部分。在这种情况下,美国用户只能运送到美国;来自加拿大的用户只能运送到加拿大等等。
如果有一种方式仍会默认为账单上的 Goelocated 国家/地区,但用户可以运送到其他国家/地区?
【问题讨论】:
-
顺便说一下您的网站证书无效。
-
我的回答对你有帮助吗?如果有,请勾选它。先生花了一些时间弄清楚一切。
-
是的,先生!它就像一个魅力!
标签: php ajax wordpress woocommerce