admin管理员组文章数量:1122846
I would like to show/hide a fee in the WooCommerce checkout process depending on the location of the user.
For this i would like to first use the IP address of the user to determine the location and later the address he enters.
I came across the build-in Geolocation ability of WooCommerce with the following classes:
- WC_Geo_IP (.html)
- WC_Geo_IP_Record (.html)
- WC_Geolocation (.html)
I'm able to get the users IP address with:
$geolocation = new WC_Geolocation();
$ipaddress = $geolocation->get_ip_address();
But i don't seem to be able to get the users country code. How would i go about this?
I would like to show/hide a fee in the WooCommerce checkout process depending on the location of the user.
For this i would like to first use the IP address of the user to determine the location and later the address he enters.
I came across the build-in Geolocation ability of WooCommerce with the following classes:
- WC_Geo_IP (https://docs.woocommerce.com/wc-apidocs/class-WC_Geo_IP.html)
- WC_Geo_IP_Record (https://docs.woocommerce.com/wc-apidocs/class-WC_Geo_IP_Record.html)
- WC_Geolocation (https://docs.woocommerce.com/wc-apidocs/class-WC_Geolocation.html)
I'm able to get the users IP address with:
$geolocation = new WC_Geolocation();
$ipaddress = $geolocation->get_ip_address();
But i don't seem to be able to get the users country code. How would i go about this?
Share Improve this question asked Jul 25, 2017 at 7:51 kleinermannkleinermann 238 bronze badges1 Answer
Reset to default 0This is how I used WooCommerce to get the country code on a recent project, using the geolocate_ip
method of the WC_Geolocation
class.
$country = 'US';
if (class_exists('WC_Geolocation')) {
$location = WC_Geolocation::geolocate_ip('', true, false);
if (isset($location['country'])) {
$country = $location['country'];
}
}
switch ($country) {
case 'AU':
break;
case 'US':
break:
}
本文标签: geo dataUnderstanding WooCommerce BuildIn Geolocation GeoIP classes
版权声明:本文标题:geo data - Understanding WooCommerce Build-In Geolocation Geo_IP classes 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287827a1927966.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论