Get user geolocated country name in Woocommerce 3
Asked Answered
Q

1

10

I would like to add "We ship to {country name}" in WooCommerce header based on user geoip country name?

I would like to write html content in the header of my WooCommerce store, such as We ship to "Your Country name".

Any help will be really appreciated?

I have WooCommerce geolocation enabled already.

Quadriplegia answered 10/7, 2018 at 22:58 Comment(0)
R
18

You can make a custom function based on WC_Geolocation Class this way:

function get_user_geo_country(){
    $geo      = new WC_Geolocation(); // Get WC_Geolocation instance object
    $user_ip  = $geo->get_ip_address(); // Get user IP
    $user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
    $country  = $user_geo['country']; // Get the country code
    return WC()->countries->countries[ $country ]; // return the country name

}

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


USAGE

You will add the following code in your child theme's header.php file:

1) in between html code:

<?php printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() ); ?>

2) or in between php code:

printf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', get_user_geo_country() );

Converting this to Shortcode:

function get_user_geo_country(){
    $geo      = new WC_Geolocation(); // Get WC_Geolocation instance object
    $user_ip  = $geo->get_ip_address(); // Get user IP
    $user_geo = $geo->geolocate_ip( $user_ip ); // Get geolocated user data.
    $country  = $user_geo['country']; // Get the country code
    return sprintf( '<p>' . __('We ship to %s', 'woocommerce') . '</p>', WC()->countries->countries[ $country ] );
}
add_shortcode('geoip_country', 'get_user_geo_country');

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

Normal shortcode usage (in the backend text editor):

[geoip_country]

or in php code:

echo do_shortcode( "[geoip_country]" );
Rossiter answered 11/7, 2018 at 7:20 Comment(7)
@loiceTheAztec, Thanks for your help, I tried it and i get this error " Parse error: syntax error, unexpected '<', expecting end of file in /home/fashi103/public_html/domainname.com/wp-content/themes/custheme-child/header-v4.php on line 79Quadriplegia
i have removed the " <?php " and it work, can it be like this? or let me know how to close tag as it seem to close maybe.Quadriplegia
thanks a lot Loic , Now i am trying to get this php code into my menu, do you have any idea how can i add it to the wordpress menu?Quadriplegia
@Quadriplegia Wordpress theming is not my strenght… so I don't know for now yet and it depend of your theme. Is possible to make a shortcode with it but it is not going to help you more...Rossiter
I think i am gonna convert it to a shortcode which might be better. giving a try now. Thank You so much Loic. That was so sweet from you :)Quadriplegia
@Quadriplegia I have added the Shortcode version at the end (in case of need).Rossiter
Loic is a Genius!Milkman

© 2022 - 2024 — McMap. All rights reserved.