【发布时间】:2022-01-21 02:48:19
【问题描述】:
WooCommerce 设置了客户的默认国家/地区。它可以被禁用,设置为商店地址国家,或按地理位置设置。
我的问题是,当使用地理定位选项时,如果无法返回商店允许的国家/地区,它现在将恢复使用商店地址国家/地区。
我想要的是在地理定位失败时没有默认选择的国家/地区。
我知道这应该可以通过functions.php 中的一些代码实现。我使用过更改默认国家/地区的代码,例如找到here 的代码。它还具有测试它是否是现有客户的优势,他们的账户上有一个国家(我想包括的东西)。我还发现了一些代码可以测试地理位置是否设置了国家,例如:
function get_customer_geo_location_country(): ?string {
if ( class_exists( 'WC_Geolocation' ) ) {
$location = WC_Geolocation::geolocate_ip();
if ( isset( $location['country'] ) ) {
return $location['country'];
}
}
return null;
}
我尝试将这些东西拼凑在一起,但没有奏效。
例如
add_filter( 'default_checkout_billing_country', 'change_default_checkout_country', 10, 1 );
function change_default_checkout_country( $country ) {
// If the user already exists, don't override country
if ( WC()->customer->get_is_paying_customer() ) {
return $country;
}
elseif ( class_exists( 'WC_Geolocation' ) ) {
$location = WC_Geolocation::geolocate_ip();
if ( isset( $location['country'] ) ) {
return $location['country'];
}
}
else {
return null;
}
}
这不起作用。虽然我怀疑它很接近。我对 PHP 的了解相当基础,所以我可能犯了一些明显的错误。
【问题讨论】:
标签: php wordpress woocommerce geolocation