I have a WooCommerce shop based in UK which sells internationally. I have set up different tax rates for different countries and integrated MaxMind IP Geolocation. The price with correct tax rate by country is displayed in the basket and checkout page before I enter the shipping address. However, the shop (and single product) page still shows the base country’s taxes (UK in my case). I have used the below code (edited from this trail [How to show correct tax on basket] (How to show correct tax on basket (BEFORE checkout)?) ), hoping that it would work also for the shop page (it doesn't in my case).
add_filter( 'woocommerce_countries_base_country', 'get_user_geo_country', 1, 1);
function get_user_geo_country(){
$geo = new WC_Geolocation();
$user_ip = $geo->get_ip_address();
$user_geo = $geo->geolocate_ip( $user_ip );
$country = $user_geo['country']; // Get the country code
return WC()->countries->countries[ $country ];
}
Any guidance would be greatly appreciated (above code edited after further testing).
WooCommerce settings that I am using: Default customer Location: geolocate with cache support (I have tried also without cache support); Entered prices without taxes; Display prices in the shop, basket and checkout with taxes; Taxes calculated based on shipping address (I would like to keep this setting as is in case a customer is buying from a country but then ships to another). Thanks!