在WooCommerce中将结帐电话字段设置为特定国家/地区可选 [英] Make checkout phone field optional for specific countries in WooCommerce

查看:167
本文介绍了在WooCommerce中将结帐电话字段设置为特定国家/地区可选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当在WooCommerce结帐屏幕中选择了某个国家/地区时,我才需要显示电话(强制性).

I have to display phone as required (mandatory) only if a certain country is selected in WooCommerce checkout screen.

实时检查所选国家/地区的验证规则是什么?

What is the validation rule to check the selected country in real time?

我尝试了以下代码,该代码可用于使非必需的电话工作:

I have tried the following code, which is working for making phone non required:

add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
function wc_npr_filter_phone( $address_fields ) {
    $address_fields['billing_phone']['required'] = false;

    return $address_fields;
}

推荐答案

您最需要在客户端上使用javascript进行实时事件或实时事件…下面的代码主要是使用jQuery和一些PHP,以使仅当客户选择特定国家/地区时,才需要付费电话:

You mostly need to use javascript for real time events or live events on client side… The code below is mostly using jQuery and a bit of PHP, to make the billing phone required only when customer select specific countries:

// Making the billing phone field not required (by default)
add_filter( 'woocommerce_billing_fields', 'filter_billing_phone_field', 10, 1 );
function filter_billing_phone_field( $fields ) {
    $fields['billing_phone']['required'] = false;
    return $fields;
}

// Real time country selection actions
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_and_fields', 10, 1 );
function custom_checkout_scripts_and_fields( $checkout ) {
    $required = esc_attr__( 'required', 'woocommerce' );

    // HERE set the countries codes (2 capital letters) in this array:
    $countries = array( 'UK', 'BE', 'GE', 'IT', 'ES' );

    // Hidden field for the phone number validation
    echo '<input type="hidden"  name="billing_phone_check" id="billing_phone_check" value="0">';
    $countries_str = "'".implode( "', '", $countries )."'"; // Formatting countries for jQuery
    ?>
    <script type="text/javascript">
        (function($){
            var required = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
                countries = [<?php echo $countries_str; ?>],
                location = $('#billing_country option:selected').val(),
                phoneCheck = 'input#billing_phone_check';

            function actionRequire( actionToDo='yes', selector='' ){
                if ( actionToDo == 'yes' ) {
                    $(selector).addClass("validate-required");
                    $(selector+' label').append(required);
                } else {
                    $(selector).removeClass("validate-required");
                    $(selector+' label > .required').remove();
                }
                $(selector).removeClass("woocommerce-validated");
                $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
            }

            // Default value when loading
            actionRequire( 'no','#billing_phone_field' );
            if( $.inArray( location, countries ) >= 0  && $(phoneCheck).val() == '0' ){
                actionRequire( 'yes','#billing_phone_field' );
                $(phoneCheck).val('1');
            }

            // Live value
            $( 'form.checkout' ).on( 'change', '#billing_country', function(){
                var location = $('#billing_country option:selected').val();
                if ( $.inArray( location, countries ) >= 0 && $(phoneCheck).val() == 0 ) {
                    actionRequire( 'yes','#billing_phone_field' );
                    $(phoneCheck).val('1');
                } else if ( $(phoneCheck).val() == 1 ) {
                    actionRequire( 'no','#billing_phone_field' );
                    $(phoneCheck).val('0');
                }
            });
       })(jQuery);
        </script>
    <?php
}

// Phone number validation, when it's visible
add_action('woocommerce_checkout_process', 'billing_phone_field_process');
function billing_phone_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['billing_phone'] && $_POST['billing_phone_check'] == '1' )
        wc_add_notice( __( 'Please enter a number phone.' ), 'error' );
}

代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

经过测试,可以正常工作.

Tested and works.

相关(2019):根据运送国家/地区将Woocommerce结帐电话字段设置为可选

这篇关于在WooCommerce中将结帐电话字段设置为特定国家/地区可选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆