将 Woocommerce 商店变成地理定位国家的目录?
我正在尝试将我的 woocommerce 商店转换为取决于国家/地区的目录模式.我只想在美国显示产品的价格并添加到购物车功能.我正在尝试通过使用名为GeoIP Detection"的插件来实现这一点,其中包含以下内容PHP中的代码:
I'm trying to turn my woocommerce shop into catalog mode depends on country.I only want to show products' price and add to cart function in United States. I'm trying to achieve this by using a plugin called "GeoIP Detection" with the following code in the PHP:
/* Disable purchasing of products if the country is not United States*/
add_filter('woocommerce_is_purchasable', 'flatsome_woocommerce_is_purchasable', 10, 2);
function flatsome_woocommerce_is_purchasable($is_purchasable, $product) {
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' == $country ) {
return true;
}
}
/*filter out prices based on country.*/
add_filter( 'woocommerce_variable_sale_price_html', 'bdd_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'bdd_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'bdd_remove_prices', 10, 2 );
function bdd_remove_prices( $price, $product ) {
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' !== $country )
{$price = '';}
return $price;
}
此代码确实有效,但未按我预期的那样完全发挥作用.有时候其他国家的朋友还可以看到价格,我自己浏览网站时也发现了一些奇怪的错误,想知道是不是缓存问题.
This code does work but not fully functioning as I expected. Sometimes my friends from other countries can still see the price, and also I found some odd bug when I browse the website myself, wondering if it's just cache issue.
谁能帮我改进这段代码?这对社区也很有帮助.
Can anyone help me to maybe improve this code? This can be very helpful too for the community.
我对这个编码还有一个奇怪的问题:
Also I have an odd question with this coding:
function flatsome_woocommerce_is_purchasable($is_purchasable, $product) {
$geoip = geoip_detect2_get_info_from_current_ip();
$country = $geoip->raw[ 'country' ][ 'iso_code' ];
if ( 'US' == $country ) {
return true;
}
}
当我写进去时:
if ( 'US' !== $country ) {
return false;
}
它不起作用,所以我也很好奇为什么.
It won't work, so I'm also curious why.
推荐答案
2020 年 5 月:在 WooCommerce 4+ 上测试并也适用于可变产品
May 2020: Tested and works perfectly for variable products too on WooCommerce 4+
对于 WooCommerce,您无需为此使用任何插件
WooCommerce 已经有专用的 WC_Geolocation
类会做得更好.
WooCommerce has already the dedicated WC_Geolocation
class that will do it better.
使用 WooCommerce 的代码 WC_Geolocation
类代替:
The code using WooCommerce WC_Geolocation
class instead:
// Utility function to get geolocated user country based on WooCommerce WC_Geolocation Class
function get_geolocated_user_country(){
// 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 );
return $user_geolocation['country'];
}
// Disable purchasing of products if the country is not United States
add_filter('woocommerce_is_purchasable', 'purchasable_country_based', 10, 2);
function purchasable_country_based( $is_purchasable, $product ) {
// Enable for "US" only geolocated users country
if( get_geolocated_user_country() ==='US' )
return true;
else
return false;
}
// Filter out prices based on country
add_filter( 'woocommerce_variable_sale_price_html', 'display_prices_country_based', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'display_prices_country_based', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'display_prices_country_based', 10, 2 );
function display_prices_country_based( $price, $product ) {
// Enable for "US" only geolocated users country
if( get_geolocated_user_country() === 'US' )
return $price;
else
return '';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中.经过测试并且可以工作.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
现在,当美国客户想从其他国家/地区购买商品时,您可能会遇到一些问题,例如,如果他们正在旅行……
Now you might face some problems when US customers want to buy from another country, if they are travelling for example…
Woocommerce Geo IP 相关答案:
Woocommerce Geo IP related answer:
- 添加内联 CSS 除外Woocommerce 中的特定国家/地区
- 在 Woocommerce 中禁用基于用户国家地理 IP 的支付网关
- 更改Woocommerce中基于IP地址(地理位置)的添加到购物车按钮
相关文章