首页 文章

取消所有国家/地区的结算状态结帐字段,但Woocommerce中的特定国家/地区除外

提问于
浏览
1

在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中特定国家/地区以外的所有国家/地区的结算状态字段?

1 回答

  • 1

    您似乎希望删除除US,AU,CA和GB之外的所有国家/地区的结算状态,因此这是执行此操作的方式,但它会影响结算 and shipping 州字段(两者):

    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文件中 . 经过测试和工作 .

相关问题