簡體   English   中英

在 WooCommerce 結帳時格式化帳單電話號碼

[英]Format billing phone number on WooCommerce checkout

對於 WooCommerce 結帳帳單字段,如何使該字段需要 9 個數字並插入破折號以正確設置電話格式? 例如,它不會在電話號碼字段中輸入 3053453212,而是顯示:(305-345-3212)

基於“ 使用 jquery 格式化表單中的電話號碼”答案代碼,這里是格式化 Woocommerce 計費電話的方法:

add_action('wp_footer', 'format_checkout_billing_phone');
function format_checkout_billing_phone() {
    if ( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('#billing_phone').on( 'input focusout', function() {
            var p = $(this).val();

            p = p.replace(/[^0-9]/g, '');
            p = p.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
            $(this).val(p);
        });
    });
    </script>
    <?php
    endif;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

你可以試試這個

/** 
*  Require 9 digit number. Format to 000-000-0000 at submission
*  stackoverflow/54700465
*/

add_filter( 'woocommerce_billing_fields', 'wc_filter_phone', 10, 1 );
function wc_filter_phone( $address_fields ) {
    $address_fields['billing_phone']['min-lenght="9"'];
    $format_number = preg_replace("/^(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $address_fields);
    return $format_number;  
}

// End

此處給出了另一種對我有用的方法。

$_POST['billing_phone'] = preg_replace("/^(\d{3})(\d{3})(\d{4})$/", "$1-$2-$3", $number);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM