添加内联 CSS 除了 Woocommerce 中的特定国家
Add inline CSS except for a specific country in Woocommerce
如果国家/地区不是法国,我想在我的 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 class。相反,您可以使用 WC_Customer
class,它已经在以下挂钩函数中使用了 WC_Countries
和 WC_Geolocation
classes:
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 文件。已测试并有效。
已更新 - 如果你真的想使用 geolocation,请改用这个:
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 网站中添加 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 class。相反,您可以使用 WC_Customer
class,它已经在以下挂钩函数中使用了 WC_Countries
和 WC_Geolocation
classes:
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 文件。已测试并有效。
已更新 - 如果你真的想使用 geolocation,请改用这个:
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相关答案: