|
| 1 | +jQuery(document).ready(function(){ |
| 2 | + |
| 3 | + jQuery('body').on('country_to_state_changed', function(p1,country){ |
| 4 | + |
| 5 | + if( 'IR' == country ){ |
| 6 | + // |
| 7 | + jQuery('input#billing_city').replaceWith('<select id="billing_city" name="billing_city"></select>'); |
| 8 | + jQuery('input#shipping_city').replaceWith('<select id="shipping_city" name="shipping_city"></select>'); |
| 9 | + jQuery('select#billing_city').selectWoo(); |
| 10 | + jQuery('select#shipping_city').selectWoo(); |
| 11 | + getCities(); |
| 12 | + }else{ |
| 13 | + jQuery('#billing_city').parent().find('.select2').remove(); |
| 14 | + jQuery('#shipping_city').parent().find('.select2').remove(); |
| 15 | + jQuery('#billing_city').replaceWith('<input type="text" name="billing_city" id="billing_city" class="input-text "/>'); |
| 16 | + jQuery('#shipping_city').replaceWith('<input type="text" name="shipping_city" id="shipping_city" class="input-text "/>'); |
| 17 | + } |
| 18 | + }); |
| 19 | + |
| 20 | + function getCities(){ |
| 21 | + jQuery('#billing_state, body').change(function(){ |
| 22 | + const province_code = jQuery(this).val(); |
| 23 | + const element = jQuery('#billing_city'); |
| 24 | + if(!province_code) |
| 25 | + return; |
| 26 | + const data = { |
| 27 | + action:'get_podro_cities_by_province', |
| 28 | + //security: wp_podro_ajax_object.security, |
| 29 | + province: province_code |
| 30 | + }; |
| 31 | + |
| 32 | + ajaxRequest(data, callbackGetCities, element); |
| 33 | + |
| 34 | + }); |
| 35 | + |
| 36 | + jQuery('#shipping_state, body').change(function(){ |
| 37 | + const province_code = jQuery(this).val(); |
| 38 | + const element = jQuery('#shipping_city'); |
| 39 | + if(!province_code) |
| 40 | + return; |
| 41 | + const data = { |
| 42 | + action:'get_podro_cities_by_province', |
| 43 | + //security: wp_podro_ajax_object.security, |
| 44 | + province: province_code |
| 45 | + }; |
| 46 | + |
| 47 | + ajaxRequest(data, callbackGetCities, element); |
| 48 | + }); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + function callbackGetCities(response, element){ |
| 53 | + |
| 54 | + const provinces = JSON.parse(response); |
| 55 | + |
| 56 | + element.find('option').remove(); |
| 57 | + |
| 58 | + let cities = provinces.cities; |
| 59 | + |
| 60 | + Object.keys(cities).forEach(function(key) { |
| 61 | + console.log('Key : ' + key + ', Value : ' + cities[key]) |
| 62 | + element.append(`<option value='${key}'>${cities[key]}</option>`); |
| 63 | + |
| 64 | + }); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + function ajaxRequest(data, callback, element){ |
| 69 | + |
| 70 | + jQuery.ajax({ |
| 71 | + url: woocommerce_params.ajax_url, |
| 72 | + type: 'post', |
| 73 | + data: data, |
| 74 | + |
| 75 | + success: function( response ) { |
| 76 | + callback(response, element); |
| 77 | + } |
| 78 | + }).fail( function( response ) { |
| 79 | + console.log(response); |
| 80 | + }) |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +}); |
0 commit comments