I have a very specific question. In Belgium we need to add a BEBAT tax when you sell batteries. I'm now trying to figure out how I can tackle this.

The code below works until a certain part so I hope I'm close but I can't seem to find the issue and hope you guys can help me.

I'm checking via IP to see what country you're in. Based on country, I want to add an extra fee of (for example) .99 and add this only on the products that have a certain category (for example batteries).

But with the code below, it shows on all products even if they are not in the category.

I also need to figure out how I can add this as text on the product and show the same message on the invoice. All tips are welcome!

function add_tax_bebat() {
    $getIp      = $_SERVER['REMOTE_ADDR'];
    $ipDetails  = json_decode( @file_get_contents( "http://ipinfo.io/{$getIp}/json" ) );
    $country    = $ipDetails->country;

    if ( $country =="BE" ){
        function return_custom_price($price, $products) {
            $args = array(
                'category'  => array( 'batteries' ),
            $products = wc_get_products( $args );
            $price = ($price+0.99);

            return $price;
        add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
add_action ( 'init', 'add_tax_bebat' );

1 Answer 1


WooCommerce has already a Geo IP feature that you can use through WC_Geolocation Class.

I have revisited completely your code. To target a product category you can use WordPress has_term() function on WooCommerce Product Category custom taxonomy as follow:

function is_geo_country_belgium() {
    // 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'] === 'BE' ? true : false;

add_filter( 'woocommerce_get_price', 'change_specific_products_price', 10, 2 );
function change_specific_products_price( $price, $product ) {
    $category = 'batteries';

    if ( has_term( $category, 'product_cat', $product->get_id() ) && is_geo_country_belgium() ) {
        $price += 0.99;
    return $price;

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

  • Hi LoicTheAztec, Thank you very much for the solutions! This is a much better way to tackle it. I couldn't figure this out myself. Thanks!
    – Flint
    May 22, 2019 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.