Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
1
Indexable
Never
add_action( 'wp_footer', 'scripts_for_adding_country_prefix_on_billing_phone2' );
function scripts_for_adding_country_prefix_on_billing_phone2(){
    ?>
    <script type="text/javascript">
        ( function( $ ) {
            jQuery('.country_to_state.country_select').on('change', function(){
                var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>",
                country_code = $(this).val();
                var ajax_data = {
                    action: 'append_country_prefix_in_billing_phone',
                    country_code: $(this).val()
                };
                $.post( ajax_url, ajax_data, function( response ) { 
                    $('#b2bking_field_4175').val(response);
                });
            });
           
        } )( jQuery );
    </script>
    <?php
}

add_action( 'wp_footer', 'scripts_for_adding_country_prefix_on_billing_phone' );
function scripts_for_adding_country_prefix_on_billing_phone(){
    ?>
    <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 ) { 
                	if (jQuery('#billing_phone').val().length === 0){
                	    $('#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();
}