programing

Woocommerce 3에서 사용자의 지리 위치 국가 이름을 가져옵니다.

sourcejob 2023. 3. 15. 19:34
반응형

Woocommerce 3에서 사용자의 지리 위치 국가 이름을 가져옵니다.

사용자 지오ip 국가명을 기준으로 WooCommerce 헤더에 "We ship to"를 추가하고 싶습니다.

We ship to your Country name 의 html 콘텐츠를 WooCommerce 스토어 헤더에 쓰고 싶습니다.

도움이 필요하신가요?

이미 WooCommerce 지올로케이션을 유효하게 하고 있습니다.

다음과 같은 방법으로 클래스를 기반으로 사용자 정의 함수를 만들 수 있습니다.

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

}

코드가 기능합니다.php 파일에는 액티브한 아이 테마(또는 활성 테마).테스트 및 동작.


사용.

다음 코드를 하위 테마에 추가합니다.header.php파일:

1) HTML 코드 사이:

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

2) 또는 php 코드 사이:

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

쇼트 코드로 변환:

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');

코드가 기능합니다.php 파일에는 액티브한 아이 테마(또는 활성 테마).테스트 및 동작.

일반 쇼트 코드 사용(백엔드 텍스트에디터):

[geoip_country]

또는 php 코드:

echo do_shortcode( "[geoip_country]" );

언급URL : https://stackoverflow.com/questions/51275007/get-user-geolocated-country-name-in-woocommerce-3

반응형