doufei9946 2017-11-22 09:44
浏览 79
已采纳

在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;
}
  • 写回答

1条回答 默认 最新

  • douqilai4263 2017-11-22 13:15
    关注

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

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works.

    Related (2019): Make Woocommerce checkout phone field optional based on shipping country

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀