【问题标题】:How to hide state field on WooCommerce checkout based on country如何根据国家/地区隐藏 WooCommerce 结帐时的状态字段
【发布时间】:2021-11-22 04:30:17
【问题描述】:

基于How to remove all Woocommerce checkout billing fields without errors 答案代码,这是我的代码尝试:

add_action( 'woocommerce_before_checkout_form', 'hide_checkout_billing_country', 5 );
function hide_checkout_billing_country() {
echo '<style>#billing_state_field{display:none !important;}</style>';
}
add_filter('woocommerce_billing_fields', 'customize_checkout_fields', 100 );
function customize_checkout_fields( $fields ) {
if ( is_checkout() ) {
// HERE set the required key fields below
$chosen_fields = array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'postcode', 'country', 'state');
    foreach( $chosen_fields as $key ) {
        if( isset($fields['billing_'.$key]) && $key !== 'state') {
            unset($fields['billing_'.$key]); // Remove all define fields except country
        }
    }
}
return $fields;
}

我的目标是根据国家/地区隐藏状态字段。例如。

  • 如果 country = Italy 则显示 state 字段
  • 如果 country = France 则隐藏 state 字段..

所以我尝试插入一个 if 条件,但没有得到想要的结果。

如果有人能给我建议,将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce checkout


    【解决方案1】:

    您的代码包含一些错误和缺点

    • 您的代码尝试修改,基于州不适用于国家
    • 您的代码包含一个致命错误:“未捕获错误:call_user_func_array():参数 #1 ($callback) 必须是有效回调,未找到函数“customize_checkout_fields”或.. 我已经编辑了这个
    • 您可以使用WC()-&gt;customer-&gt;get_billing_country() 获取帐单国家/地区

    所以你得到:

    function filter_woocommerce_billing_fields( $fields ) { 
        // Returns true when viewing the checkout page
        if ( is_checkout() ) {
            // Get billing country
            $billing_country = WC()->customer->get_billing_country();
            
            // Multiple country codes can be added, separated by a comma
            $countries = array( 'IT' );
            
            // NOT in array
            if ( ! in_array( $billing_country, $countries ) ) {
                // HERE set the fields below, multiple fields can be added, separated by a comma
                $chosen_fields = array( 'state' );
                
                // Loop through
                foreach( $chosen_fields as $key ) {
                    // Isset
                    if ( isset( $fields['billing_' . $key] ) ) {
                        // Remove all defined fields
                        unset( $fields['billing_' . $key] );
                    }
                }
            }
        }
        
        return $fields;
    }
    add_filter( 'woocommerce_billing_fields', 'filter_woocommerce_billing_fields', 10, 1 );
    

    【讨论】:

    • 晚上好 7uc1f3r,首先非常感谢您的关注。我的目标是根据国家/地区隐藏状态字段。例如。如果 country == Italy 则显示 state 字段,但如果 country == France 则隐藏 state 字段。事实上,我试图重新调整上面共享的代码来实现这一点,正如你所看到的,我写了if( isset($fields['billing_'.$key]) &amp;&amp; $key !== 'state') { 试图隐藏它。我为不够清楚而道歉。热烈的问候麦芽酒
    • @AlessandroValori 我已经更新了我的答案。写清楚的问题确实很重要,信息太多总比信息太少好。这样就可以避免这样的误会
    • 早上好,抱歉回复晚了。我试过你的代码,但它不起作用。唯一有效的代码如下: function so_01_filter_woocommerce_states( $states ) { unset( $states['FR'] );返回 $states; }; add_filter('woocommerce_states', 'so_01_filter_woocommerce_states', 10, 1);功能 so_01_filter_woocommerce_get_country_locale( $locale ) { $locale['LU']['state']['required'] = true;返回$语言环境; }; add_filter('woocommerce_get_country_locale', 'so_01_filter_woocommerce_get_country_locale', 10, 1);但它只适用于一个国家
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 2016-01-01
    • 2019-04-07
    • 2020-09-15
    • 2018-09-14
    • 2015-04-09
    • 2019-08-07
    • 2015-10-27
    相关资源
    最近更新 更多