在 Woocommerce 3 中获取用户地理定位的国家名称
Get user geolocated country name in Woocommerce 3
我想根据用户 geoip 国家名称在 WooCommerce header 中添加 “我们运送到 {country name}
”?
我想在我的 WooCommerce 商店的 header 中写 html 内容,例如 We ship to "Your Country name"。
任何帮助将不胜感激?
我已经启用了 WooCommerce 地理定位。
你可以这样在WC_Geolocation
Class的基础上创建一个自定义函数:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ]; // return the country name
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法
您将在子主题的 header.php
文件中添加以下代码:
1)在html代码之间:
<?php printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() ); ?>
2) 或介于 php 代码之间:
printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() );
将其转换为简码:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return sprintf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', WC()->countries->countries[ $country ] );
}
add_shortcode('geoip_country', 'get_user_geo_country');
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
正常简码用法(在后端文本编辑器中):
[geoip_country]
或在php代码中:
echo do_shortcode( "[geoip_country]" );
我想根据用户 geoip 国家名称在 WooCommerce header 中添加 “我们运送到 {country name}
”?
我想在我的 WooCommerce 商店的 header 中写 html 内容,例如 We ship to "Your Country name"。
任何帮助将不胜感激?
我已经启用了 WooCommerce 地理定位。
你可以这样在WC_Geolocation
Class的基础上创建一个自定义函数:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ]; // return the country name
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
用法
您将在子主题的 header.php
文件中添加以下代码:
1)在html代码之间:
<?php printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() ); ?>
2) 或介于 php 代码之间:
printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() );
将其转换为简码:
function get_user_geo_country(){
$geo = new WC_Geolocation(); // Get WC_Geolocation instance object
$user_ip = $geo->get_ip_address(); // Get user IP
$user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
$country = $user_geo['country']; // Get the country code
return sprintf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', WC()->countries->countries[ $country ] );
}
add_shortcode('geoip_country', 'get_user_geo_country');
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
正常简码用法(在后端文本编辑器中):
[geoip_country]
或在php代码中:
echo do_shortcode( "[geoip_country]" );