問題描述
在我的 Woocommerce 商店中,我設置了地理定位系統,當地理定位識別出 IT 以外的任何國家/地區時,我想禁用付款方式
In my Woocommerce shop I set up the geolocation system, when geolocation identifies any country other than IT I would like to disable payment methods
如果是 IT (geop-ip),請顯示付款方式
If it is IT (geop-ip), show payment methods
如果是所有其他國家/地區 (geo-ip),請禁用所有付款方式.
If all other country (geo-ip), disable all payment methods.
推薦答案
Woocommerce 通過 /class-WC_Geolocation.html" rel="nofollow noreferrer">WC_Geolocation
類,所以你不需要任何額外的插件.
Woocommerce has already a geolocation Ip feature through WC_Geolocation
class, so you don't need any additional plugin.
以下是禁用除IT"以外的所有國家/地區的支付網關的方法;(意大利)國家/地區代碼,基于客戶地理定位的 IP 國家/地區:
Here is the way to disable payment gateways for all countries except "IT" (Italy) country code, based on costumer geolocated IP country:
// Disabling payment gateways except for the defined country codes based on user IP geolocation country
add_filter( 'woocommerce_available_payment_gateways', 'geo_country_based_available_payment_gateways', 90, 1 );
function geo_country_based_available_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// ==> HERE define your country codes
$allowed_country_codes = array('IT');
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// Disable payment gateways for all countries except the allowed defined coutries
if ( ! in_array( $user_geolocation['country'], $allowed_country_codes ) )
$available_gateways = array();
return $available_gateways;
}
代碼位于活動子主題(或活動主題)的 functions.php 文件中.經測試有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
相關:
- 禁用 WooCommerce 支付網關來賓和特定用戶角色
- 啟用基于客戶位置的付款方式
- 隱藏基于特定付款方式Woocommerce 的總重量
- 根據產品類型隱藏付款方式在 WooCommerce 中
這篇關于在 WooCommerce 中禁用所有基于用戶國家地理 IP 的支付方式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!