问题
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;
}
}
When I write in:
if ( 'US' !== $country ) {
return false;
}
It won't work, so I'm also curious why.
回答1:
May 2020: Tested and works perfectly for variable products too on WooCommerce 4+
For WooCommerce, You don't need to use any plugin for that
WooCommerce has already the dedicated WC_Geolocation class that will do it better.
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 '';
}
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 related answer:
- Add inline CSS except for a specific country in Woocommerce
- Disable payment gateways based on user country geo-ip in Woocommerce
- Change add to cart button based on IP-adress (GeoLocation) in Woocommerce
来源:https://stackoverflow.com/questions/51251140/turn-woocommerce-shop-into-catalog-for-geolocated-countries