如何在wordpress中显示国家/地区电话代码?

时间:2019-09-03 05:07:49

标签: wordpress woocommerce

我无法编辑woocommerce chekcout页面。

我想添加我的国家电话号码代码,例如(+88),但我无法执行此操作。

更新:

我从referece处尝试了此代码,并将以下代码添加到了我的主要主题(woodmart)function.php中。

wp_enqueue_style( 'int-tel-phone-style', 'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/6.4.1/css/intlTelInput.css' );
wp_enqueue_script('int-tel-phone-js','https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/6.4.1/js/intlTelInput.min.js');

最后将以下代码脚本添加到footer.php中

<script type="text/javascript">
  $("#billing_phone").intlTelInput();
</script>

但是它仍然无法正常工作。 有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

只需在活动主题的functions.php中添加以下代码片段即可完成工作-

add_action( 'wp_footer', 'callback_wp_footer' );
function callback_wp_footer(){
    ?>
    <script type="text/javascript">
        ( function( $ ) {
            $( document.body ).on( 'updated_checkout', function(data) {
                var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>",
                country_code = $('#billing_country').val();

                var ajax_data = {
                    action: 'append_country_prefix_in_billing_phone',
                    country_code: $('#billing_country').val()
                };

                $.post( ajax_url, ajax_data, function( response ) { 
                    $('#billing_phone').val(response);
                });
            } );
        } )( jQuery );
    </script>
    <?php
}

add_action( 'wp_ajax_nopriv_append_country_prefix_in_billing_phone', 'country_prefix_in_billing_phone' );
add_action( 'wp_ajax_append_country_prefix_in_billing_phone', 'country_prefix_in_billing_phone' );
function country_prefix_in_billing_phone() {
    $calling_code = '';
    $country_code = isset( $_POST['country_code'] ) ? $_POST['country_code'] : '';
    if( $country_code ){
        $calling_code = WC()->countries->get_country_calling_code( $country_code );
        $calling_code = is_array( $calling_code ) ? $calling_code[0] : $calling_code;

    }
    echo $calling_code;
    die();
}