Skip to content

Commit b1f154b

Browse files
authored
Merge pull request #20 from KhorshidLab/FIX-Pod-50-city-select-conflict
Fix pod 50 city select conflict
2 parents cbe5aea + 0134be7 commit b1f154b

File tree

5 files changed

+119
-9
lines changed

5 files changed

+119
-9
lines changed

admin/Enqueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function enqueue_admin_scripts() {
7575
public function enqueue_public_styles() {
7676

7777
if ( is_cart() || is_checkout() || is_wc_endpoint_url( 'edit-address' ) ) {
78-
$city_select_path = PODRO_PLUGIN_ROOT_URL . 'assets/js/city-select.js';
78+
$city_select_path = PODRO_PLUGIN_ROOT_URL . 'assets/js/cities.js';
7979
wp_enqueue_script(
8080
'wc-city-select',
8181
$city_select_path,

assets/js/cities.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
});

assets/js/city-select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ jQuery( function($) {
157157
$citybox = $('#'+input_id);
158158
} else {
159159
$citybox.prop( 'disabled', false );
160-
jQuery('#billing_city').removeClass('city_select').addClass('city_select');
160+
//jQuery('#billing_city').removeClass('city_select').addClass('city_select');
161161
}
162162

163163
var options = '';

inc/MetaBox.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ public function delivery_step_1( $order ) {
137137

138138
$source_city = $woo_setting->get_store_city_code_from_options();
139139
$order_id = $order->get_id();
140-
$destination_city = $order->get_shipping_city();
141-
$destination_city = Location::get_city_by_name($destination_city);
142-
$destination_address = $destination_city['name'] . ' ' . $order->get_billing_address_1() . ' ' . $order->get_billing_address_2();
140+
$destination_city_code = $order->get_shipping_city();
141+
142+
$destination_city_name = (WooSetting::get_instance())->get_cities()[$destination_city_code];
143+
$destination_address = $destination_city_name . ' ' . $order->get_billing_address_1() . ' ' . $order->get_billing_address_2();
143144
if( mb_strlen($destination_address) > $this->address_length )
144145
$destination_address = mb_substr($destination_address, 0, $this->address_length);
145146
$store_address = $woo_setting->get_store_city() . ' ' . get_option( 'woocommerce_store_address' ) . get_option( 'woocommerce_store_address_2' );
@@ -206,11 +207,11 @@ public function delivery_step_1( $order ) {
206207
</li>
207208
<li>
208209
<label for="pod_destination_city">مقصد</label>
209-
<?php if( !Location::is_podro_city($destination_city['code']) ){ ?>
210+
<?php if( !Location::is_podro_city($destination_city_code) ){ ?>
210211
<span style="color:red">این شهر پادرویی نیست</span>
211212
<?php } ?>
212213
<textarea name="pod_destination_city" id="pod_destination_city" rows="6" maxlength="186"><?php echo esc_attr($destination_address); ?></textarea>
213-
<input type="hidden" id="pod_destination_city_code" name="pod_destination_city_code" value="<?php echo esc_attr($destination_city['code']); ?>">
214+
<input type="hidden" id="pod_destination_city_code" name="pod_destination_city_code" value="<?php echo esc_attr($destination_city_code); ?>">
214215
</li>
215216
<li>
216217
<label for="pod_user_billing_name">نام </label>
@@ -221,7 +222,6 @@ public function delivery_step_1( $order ) {
221222
<input type="text" name="pod_user_billing_family" id="pod_user_billing_family" maxlength="27" value="<?php echo esc_attr($user_billing_family); ?>" />
222223
</li>
223224
<li>
224-
225225
<label for="pod_comment">توضیحات<span id="pod-description-hint" class="dashicons dashicons-editor-help" style="width:50px"></span></label>
226226
<textarea name="pod_customer_note" id="pod_customer_note" rows="6" maxlength="60"><?php echo esc_attr($customer_note); ?></textarea>
227227
</li>
@@ -375,7 +375,6 @@ public function ajax_saving_options_step_2() {
375375

376376
$destination_city_code = get_option('pod_destination_city_code');
377377

378-
379378
$pod_store_name = get_option('pod_store_name');
380379
$pod_user_billing_name = get_option('pod_user_billing_name') . ' ' . get_option('pod_user_billing_family');
381380
$pod_customer_note = get_option('pod_customer_note');

inc/Setup.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ private function load_dependencies() {
150150
// Register shipping method
151151
require_once( PODRO_PLUGIN_ROOT . 'WC/Shipping_Method.php' );
152152

153+
154+
add_action( 'wp_ajax_nopriv_get_podro_cities_by_province', function(){
155+
156+
$provinces = WooSetting::get_provinces();
157+
$province_code = $_POST['province'];
158+
foreach ($provinces as $province){
159+
if($province['code'] == $province_code)
160+
{
161+
wp_send_json( wp_json_encode($province));
162+
wp_die();
163+
}
164+
}
165+
166+
} );
167+
add_action( 'wp_ajax_get_podro_cities_by_province', function(){
168+
169+
$provinces = WooSetting::get_provinces();
170+
$province_code = $_POST['province'];
171+
foreach ($provinces as $province){
172+
if($province['code'] == $province_code)
173+
{
174+
wp_send_json( wp_json_encode($province));
175+
wp_die();
176+
}
177+
}
178+
179+
} );
153180
}
154181

155182
/**

0 commit comments

Comments
 (0)