반응형
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
반응형
'programing' 카테고리의 다른 글
| Javascript를 사용하여 JSON 파일에 데이터를 쓰는 방법 (0) | 2023.03.15 |
|---|---|
| 하위 구성 요소의 업데이트 상태에 대한 변경 이벤트 (0) | 2023.03.15 |
| 브라우저에서 각도 서비스를 디버깅하는 방법 (0) | 2023.03.15 |
| wordpress는 localhost에 복사한 후 항상 https로 리디렉션됩니다. (0) | 2023.03.15 |
| 반응 - useState - setTimeout 함수에 최신 상태 값이 없는 이유는 무엇입니까? (0) | 2023.03.15 |