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.