本文介绍了添加内联CSS(Woocommerce中特定国家/地区除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果国家/地区不是法国,我想在我的woocommerce网站中添加CSS样式,因为我需要在除法国以外的所有国家/地区中隐藏一个按钮.我尝试了下面的代码

I would like to add css style in my woocommerce website if country is not France because i need to hide a button in all countries except France. I tried the code below

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_CountriesWC_Geolocation类:

You don't really need to use WC_Geolocation class for this. Instead you can use WC_Customer class which already uses the WC_Countries and WC_Geolocation classes in following hooked function:

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

已更新-如果您确实要使用地理位置,请使用以下替代方法:

Updated - If you really want to use geolocation, use this instead:

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文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

与Geo IP相关的答案:

Geo IP related answer:

  • Disable payment gateways based on user country geo-ip in Woocommerce
  • Change add to cart button based on IP-adress (GeoLocation) in Woocommerce

这篇关于添加内联CSS(Woocommerce中特定国家/地区除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:03
查看更多