【问题标题】:Add inline CSS except for a specific country in Woocommerce在 Woocommerce 中添加除特定国家/地区以外的内联 CSS
【发布时间】:2018-12-03 23:48:22
【问题描述】:

如果国家/地区不是法国,我想在我的 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;
}

【问题讨论】:

    标签: php css wordpress woocommerce geolocation


    【解决方案1】:

    您实际上不需要为此使用 WC_Geolocation 类。相反,您可以使用 WC_Customer 类,它已经在以下挂钩函数中使用了 WC_CountriesWC_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 文件中。经过测试并且可以工作。


    更新 - 如果您真的想使用 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相关答案:

    【讨论】:

    • 不适合我! if( ! country_geo_ip_check( array('FR') ) ) 我应该在 country_geo_ip_check 之前添加 $ 吗?
    • @Elichy 好的,只需再次进行一些更改。这次它按预期工作得很好。
    猜你喜欢
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2014-04-29
    • 2019-05-04
    相关资源
    最近更新 更多