Добавить встроенный CSS за исключением определенной страны в Woocommerce - PullRequest
0 голосов
/ 25 июня 2018

Я хотел бы добавить стиль css на моем веб-сайте woocommerce, если страна не является Францией, потому что мне нужно скрыть кнопку во всех странах, кроме Франции.Я попробовал код ниже

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;
}

1 Ответ

0 голосов
/ 25 июня 2018

Вам не нужно использовать класс WC_Geolocation для этого. Вместо этого вы можете использовать класс WC_Customer, который уже использует классы WC_Countries и 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 вашей активной дочерней темы (или активной темы). Проверено и работает.


Обновлено - Если вы действительно хотите использовать геолокация , используйте вместо этого:

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 вашей активной дочерней темы (или активной темы). Проверено и работает.

Geo IP связанный ответ:

...