将 Woocommerce 商店变成地理定位国家/地区的目录?
Turn Woocommerce shop into catalog for geolocated countries?
我正在尝试将我的 woocommerce 商店转为目录模式,这取决于 country.I 只想在美国显示产品价格并添加到购物车功能。我试图通过在 PHP:
中使用名为“GeoIP Detection”的插件和以下代码来实现此目的
/* 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;
}
这段代码确实有效,但没有像我预期的那样完全发挥作用。有时其他国家的朋友还能看到价格,我自己浏览网站时也发现了一些奇怪的错误,想知道是不是缓存问题。
谁能帮我改进这段代码?这对社区也很有帮助。
另外我对这个编码有一个奇怪的问题:
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;
}
不行,我也很好奇为什么。
May 2020: Tested and works perfectly for variable products too on WooCommerce 4+
对于 WooCommerce,您不需要为此使用任何插件
WooCommerce 已经有专门的 WC_Geolocation
class 可以做得更好。
代码使用 WooCommerce WC_Geolocation
class 代替:
// 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 文件。已测试并有效。
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 商店转为目录模式,这取决于 country.I 只想在美国显示产品价格并添加到购物车功能。我试图通过在 PHP:
中使用名为“GeoIP Detection”的插件和以下代码来实现此目的/* 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;
}
这段代码确实有效,但没有像我预期的那样完全发挥作用。有时其他国家的朋友还能看到价格,我自己浏览网站时也发现了一些奇怪的错误,想知道是不是缓存问题。
谁能帮我改进这段代码?这对社区也很有帮助。
另外我对这个编码有一个奇怪的问题:
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;
}
不行,我也很好奇为什么。
May 2020: Tested and works perfectly for variable products too on WooCommerce 4+
对于 WooCommerce,您不需要为此使用任何插件
WooCommerce 已经有专门的 WC_Geolocation
class 可以做得更好。
代码使用 WooCommerce WC_Geolocation
class 代替:
// 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 文件。已测试并有效。
Now you might face some problems when US customers want to buy from another country, if they are travelling for example…
Woocommerce Geo IP 相关答案: