【问题标题】:Unset billing state checkout field for all countries except specific ones in Woocommerce取消设置除 Woocommerce 中特定国家以外的所有国家/地区的计费状态结帐字段
【发布时间】:2018-04-04 10:28:34
【问题描述】:

在 Woocommerce 中,我正在尝试取消设置所有国家/地区的帐单状态字段,结帐页面上的特定国家除外……

这是我的代码:

add_filter( 'woocommerce_checkout_fields','custom_override_default_address_fields' );
function custom_override_default_address_fields($myfields){
  global $woocommerce;
  $country = $woocommerce->customer->get_billing_country();
  if($country !== 'US' || $country !== 'AU' || $country !== 'CA' || $country !== 'GB'){
    unset( $myfields['billing']['billing_state'] );
  }
  return $myfields;
}

但是真的不行……

如何删除除 woocommerce 中特定国家之外的所有国家/地区的帐单状态字段?

【问题讨论】:

    标签: php wordpress woocommerce checkout states


    【解决方案1】:

    您似乎想删除除美国、澳大利亚、加拿大和英国以外的所有国家/地区的帐单状态,所以这是执行此操作的方法,但它会影响帐单和运输状态字段(两个):

    add_filter( 'woocommerce_states' , 'keep_specific_country_states', 10, 1 );
    function keep_specific_country_states( $states ) {
        // HERE define the countries where you want to keep
        $countries = array('US', 'AU', 'CA', 'GB');
        $new_country_states = array();
    
        // Loop though all country states
        foreach( $states as $country_code => $country_states ){
            if( ! in_array( $country_code, $countries ) ){
                // Remove states from all countries except the defined ones
                $states[$country_code] = array();
            }
        }
        return $states;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

    【讨论】:

      猜你喜欢
      • 2019-04-07
      • 2017-05-31
      • 2016-01-01
      • 2018-05-05
      • 2015-04-09
      • 2021-10-27
      • 2021-11-22
      • 2019-08-13
      • 2020-08-28
      相关资源
      最近更新 更多