Php 将Woocommerce商店变成地理位置国家的目录?
我正在尝试将我的woocommerce商店变成目录模式,具体取决于国家。我只想显示产品的价格,并添加到美国的购物车功能。我试图通过在PHP中使用名为“”的插件和以下代码来实现这一点:Php 将Woocommerce商店变成地理位置国家的目录?,php,wordpress,woocommerce,geolocation,geoip,Php,Wordpress,Woocommerce,Geolocation,Geoip,我正在尝试将我的woocommerce商店变成目录模式,具体取决于国家。我只想显示产品的价格,并添加到美国的购物车功能。我试图通过在PHP中使用名为“”的插件和以下代码来实现这一点: /* Disable purchasing of products if the country is not United States*/ add_filter('woocommerce_is_purchasable', 'flatsome_woocommerce_is_purchasable', 10, 2)
/* 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;
}
这行不通,所以我也很好奇为什么
2020年5月:在WooCommerce 4上测试并适用于各种产品+
对于WooCommerce,您不需要使用任何插件
WooCommerce已经有了专门的课程,可以做得更好
代码将改为使用WooCommerce类:
// 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文件。测试和工作
现在,当美国客户想要从另一个国家购买时,您可能会遇到一些问题,例如,如果他们正在旅行
与知识产权相关的回答: