WooCommerce 設定了客戶的默認國家/地區。它可以被禁用,設定為商店地址國家,或按地理位置設定。
我的問題是,當使用地理定位選項時,如果未能回傳商店允許的國家/地區,它現在將恢復為使用商店地址國家/地區。
我想要的是在地理定位失敗時沒有默認選擇的國家。
我知道這應該可以通過functions.php中的一些代碼來實作。我使用過更改默認國家/地區的代碼,例如在此處找到的代碼。它還具有測驗是否是現有客戶的優勢,他們的帳戶上有一個國家/地區(我想包括這一點)。我還發現了一些代碼可以測驗地理位置是否設定了國家,例如:
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;
}
來源:WooCommerce:在賬單中設定默認國家/地區(使用地理位置),但不在結帳頁面答案代碼中的運輸中。
我嘗試將這些東西拼湊在一起,但沒有奏效。
例如
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 的了解相當基礎,所以我可能犯了一些明顯的錯誤。
uj5u.com熱心網友回復:
就其本身而言,您的代碼沒有任何問題,只有滿足 elseif 條件,而不是 elseif 中的 if 條件,您不應該假設執行 else 條件。
基本上,您的 elseif 條件中缺少一個 else
所以你得到:
function filter_default_checkout_billing_country( $default ) {
// If the user already exists, don't override country
if ( WC()->customer->get_is_paying_customer() ) {
return $default;
} elseif ( class_exists( 'WC_Geolocation' ) ) {
// Get location country
$location = WC_Geolocation::geolocate_ip();
if ( isset( $location['country'] ) ) {
return $location['country'];
} else {
$default = null;
}
} else {
$default = null;
}
return $default;
}
add_filter( 'default_checkout_billing_country', 'filter_default_checkout_billing_country', 10, 1 );
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418956.html
標籤:
