3

I am currently trying to add on my homepage a custom message based on the location of the visitor. If the visitor have already been to the checkout and choosed shipping Country, then the message will be based on this country. Otherwise we will use the WooCommerce geolocation function.

function get_user_country(){
    if( isset($woocommerce->customer) ){
        $country = $woocommerce->customer->get_shipping_country();
    }
    else{
        $geo      = new WC_Geolocation(); 
        $user_ip  = $geo->get_ip_address(); 
        $user_geo = $geo->geolocate_ip( $user_ip ); 
        $country  = $user_geo['country']; 
    }
    if( $country == 'CA' ){
        echo 'We offer free shipping to Canada';
   }
}

My issue is that it seems that the result is store in cache. When refreshing my homepage, the message is not updated. And I don't want to exclude my homepage from caching.

I have read that one way to get the country dynamically is to use Ajax instead of php. But I am a beginner in web development and I am afraid this out of my knowledge for now... is there any other way to resolve my issue? Thanks.

Schmago
  • 31
  • 2

0 Answers0