我希望根据用户的地理位置国家名称,在WooCommerce页眉中添加“我们发送到
{国家名称}
”。
我想在我的WooCommerce商店的页眉中编写HTML内容,例如我们发送到“您的国家名称”。
如果有任何帮助,将非常感激?
我已经启用了WooCommerce地理定位。
{国家名称}
”。
我想在我的WooCommerce商店的页眉中编写HTML内容,例如我们发送到“您的国家名称”。
如果有任何帮助,将非常感激?
我已经启用了WooCommerce地理定位。
你可以这样基于WC_Geolocation
类来创建自定义函数:
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
}
将代码添加到您活动的子主题(或活动主题)的function.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');
将代码放入您的活动子主题(或活动主题)的function.php文件中。 已测试并且有效。
在后端文本编辑器中的常规简码用法 :
[geoip_country]
或者在 PHP 代码中:
echo do_shortcode( "[geoip_country]" );