如果国家/地区不是法国,我想在我的 woocommerce 网站中添加 css 样式,因为我需要在除法国之外的所有国家/地区隐藏按钮。我尝试了下面的代码
add_filter( 'woocommerce_state_FR' , 'custom_css_countries', 10, 1 );
function custom_css_countries($mycss) {
$country = array('FR');
if( ! in_array( $country ) ){
echo '<style>#payez-sur-12-mois{display:none;}</style>';
}
return $mycss;
}
您实际上并不需要为此使用 WC_Geolocation 类。相反,你可以使用WC_Customer
已经使用的类WC_Countries
and WC_Geolocation
以下挂钩函数中的类:
add_action( 'wp_head' , 'custom_inline_css', 200 );
function custom_inline_css() {
// For all countries except 'FR'
if( WC()->customer->get_billing_country() !== 'FR' )
?><style>#payez-sur-12-mois{display:none !important;}</style><?php
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
Updated- 如果你真的想使用地理定位,用这个代替:
add_action( 'wp_head' , 'custom_inline_css', 200 );
function custom_inline_css() {
// Get an instance of the WC_Geolocation object class
$geolocation_instance = new WC_Geolocation();
// Get user IP
$user_ip_address = $geolocation_instance->get_ip_address();
// Get geolocated user IP country code.
$user_geolocation = $geolocation_instance->geolocate_ip( $user_ip_address );
// For all countries except 'FR'
if( $user_geolocation['country'] !== 'FR' ){
?><style>#payez-sur-12-mois{display:none !important;}</style><?php
}
// For testing (to be removed):
echo '<!-- GEO Located country: '.$user_geolocation['country'].' -->';
}
代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。
地理IP相关答案:
- 在 Woocommerce 中禁用基于用户国家/地区地理 IP 的支付网关 https://stackoverflow.com/questions/49954740/disable-payment-gateways-based-on-user-country-geo-ip-in-woocommerce/49955799?s=1%7C0.0000#49955799
- 根据 Woocommerce 中的 IP 地址(地理位置)更改“添加到购物车”按钮 https://stackoverflow.com/questions/49793939/change-add-to-cart-button-based-on-ip-adress-geolocation-in-woocommerce/49801968#49801968
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)