-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
730 lines (694 loc) · 29.8 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
<?php
/**
* Functions.
*
* @package Kijam
*/
add_filter(
'wc_order_statuses',
function ( $order_statuses ) {
$new_order_statuses = array();
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-arrival'] = __( 'Shipment Arrival', 'carriers-of-argentina-for-woocommerce' );
$new_order_statuses['wc-intransit'] = __( 'Shipment in Transit', 'carriers-of-argentina-for-woocommerce' );
}
}
return $new_order_statuses;
}
);
add_filter(
'woocommerce_register_shop_order_post_statuses',
function ( $order_statuses ) {
$order_statuses['wc-arrival'] = array(
'label' => __( 'Shipment Arrival', 'carriers-of-argentina-for-woocommerce' ),
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
// translators: count.
'label_count' => _n_noop( 'Shipment Arrival <span class="count">(%s)</span>', 'Shipment Arrival <span class="count">(%s)</span>' ),
);
$order_statuses['wc-intransit'] = array(
'label' => __( 'Shipment in Transit', 'carriers-of-argentina-for-woocommerce' ),
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
// translators: count.
'label_count' => _n_noop( 'Shipment in Transit <span class="count">(%s)</span>', 'Shipment in Transit <span class="count">(%s)</span>' ),
);
return $order_statuses;
}
);
add_filter(
'woocommerce_states',
function ( $states ) {
$states['AR'] = array(
'C' => 'Capital Federal',
'B' => 'Buenos Aires',
'K' => 'Catamarca',
'H' => 'Chaco',
'U' => 'Chubut',
'X' => 'Córdoba',
'W' => 'Corrientes',
'E' => 'Entre Ríos',
'P' => 'Formosa',
'Y' => 'Jujuy',
'L' => 'La Pampa',
'F' => 'La Rioja',
'M' => 'Mendoza',
'N' => 'Misiones',
'Q' => 'Neuquén',
'R' => 'Río Negro',
'A' => 'Salta',
'J' => 'San Juan',
'D' => 'San Luis',
'Z' => 'Santa Cruz',
'S' => 'Santa Fe',
'G' => 'Santiago del Estero',
'V' => 'Tierra del Fuego',
'T' => 'Tucumán',
);
return $states;
}
);
add_action(
'woocommerce_checkout_update_order_review',
function ( $posted_data ) {
if ( ! WC()->session ) {
WC()->session = new WC_Session_Handler();
WC()->session->init();
}
$data = WC()->session->get_session_data();
foreach ( $data as $key => $v ) {
if ( strstr( $key, 'shipping_for_package' ) !== false ) {
unset( WC()->session->$key );
}
}
WC()->cart->calculate_totals();
WC()->cart->calculate_shipping();
}
);
add_action(
'woocommerce_after_shipping_rate',
function ( $method, $index ) {
if ( ! is_checkout() ) {
return; // Only on checkout page.
}
if ( false === strstr( $method->id, 'kshippingargentina' ) ) {
return;
}
$t_setting = get_option( 'woocommerce_kshippingargentina-shipping_' . $method->instance_id . '_settings' );
if ( ! isset( $t_setting['type'] ) || 'office' !== $t_setting['type'] ) {
return;
}
$office_name = WC_KShippingArgentina::woocommerce_instance()->checkout->get_value( 'kshippingargentina_method_office_name' );
echo '<div data-instance_id="' . esc_html( $method->instance_id ) . '" class="custom-office_kshippingargentina method_instance_id-' . esc_html( $method->instance_id ) . '" style="display: flex;padding-left: 20px;">
<input class="method_nonce" name="kshippingargentina_method_nonce[' . esc_html( $method->instance_id ) . ']" value="' . esc_html( wp_create_nonce( 'office_kshippingargentina_' . $method->instance_id ) ) . '" type="hidden" />
<input class="method_instance_id" value="' . esc_html( $method->instance_id ) . '" type="hidden" />
<input class="method_office_name" name="kshippingargentina_method_office_name[' . esc_html( $method->instance_id ) . ']" type="hidden" value="' . esc_html( $office_name && isset( $office_name[ $method->instance_id ] ) ? $office_name[ $method->instance_id ] : '' ) . '" />
';
$values = WC_KShippingArgentina::woocommerce_instance()->checkout->get_value( 'kshippingargentina_method_office' );
woocommerce_form_field(
'kshippingargentina_method_office[' . $method->instance_id . ']',
array(
'label' => __( 'Nearest office', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'select',
'class' => array( 'form-row-wide office_kshippingargentina' ),
'required' => false,
'options' => array(
'' => __( 'Choose one...', 'carriers-of-argentina-for-woocommerce' ),
),
),
isset( $values[ $method->instance_id ] ) ? $values[ $method->instance_id ] : ''
);
echo '</div>';
},
20,
2
);
add_action(
'woocommerce_checkout_update_order_meta',
function ( $order_id ) {
$zones = WC_Shipping_Zones::get_zones();
$methods = array_map(
function ( $zone ) {
return $zone['shipping_methods'];
},
$zones
);
$order = wc_get_order( $order_id );
$offices = $order->get_meta( '_office_kshippingargentina', true );
if ( ! $offices ) {
$offices = array();
}
if ( isset( $_POST['kshippingargentina_method_nonce'] ) ) {
foreach ( $_POST['kshippingargentina_method_nonce'] as $instance_id => $nonce ) {
if ( ! wp_verify_nonce( $nonce, 'office_kshippingargentina_' . $instance_id ) ) {
continue;
}
$shipping = WC_KShippingArgentina_Shipping::get_instance( $instance_id );
$offices[ $instance_id ] = array(
'instance_id' => $instance_id,
'service_type' => $shipping->service_type ?? '',
'office_name' => isset( $_POST['kshippingargentina_method_office_name'][ $instance_id ] ) ? sanitize_text_field( wp_unslash( $_POST['kshippingargentina_method_office_name'][ $instance_id ] ) ) : '',
'office' => isset( $_POST['kshippingargentina_method_office'][ $instance_id ] ) ? sanitize_text_field( wp_unslash( $_POST['kshippingargentina_method_office'][ $instance_id ] ) ) : '',
);
}
}
KShippingArgentina_API::debug( 'woocommerce_checkout_update_order_meta: ', array( $order_id, $offices ) );
$order->update_meta_data( '_office_kshippingargentina', $offices );
$order->save();
},
30,
1
);
// Our hooked in function - $fields is passed via the filter!
add_filter(
'woocommerce_checkout_fields',
function ( $fields ) {
if ( ! WC()->session ) {
WC()->session = new WC_Session_Handler();
WC()->session->init();
}
$ship_to_different_address = (bool) WC()->session->get( 'ship_to_different_address' );
if ( ! WC()->customer ) {
$country = 'AR';
} else {
$country = $ship_to_different_address ? WC()->customer->get_shipping_country() : WC()->customer->get_billing_country();
}
$required = true;
$bp_state = $fields['billing']['billing_state']['priority'];
$fields['billing']['billing_country']['priority'] = $bp_state - 5;
$sp_state = $fields['shipping']['shipping_state']['priority'];
$fields['shipping']['shipping_country']['priority'] = $sp_state - 5;
$bp_country = $fields['billing']['billing_country']['priority'];
$bp_address = $fields['billing']['billing_address_1']['priority'];
$bp_postcode = $fields['billing']['billing_postcode']['priority'];
$sp_address = $fields['shipping']['shipping_address_1']['priority'];
$bp_address2 = $fields['billing']['billing_address_2']['priority'];
$sp_address2 = $fields['shipping']['shipping_address_2']['priority'];
$bp_city = $fields['billing']['billing_city']['priority'];
$sp_city = $fields['shipping']['shipping_city']['priority'];
$sp_postcode = $fields['shipping']['shipping_postcode']['priority'];
$bp_phone = $fields['billing']['billing_phone']['priority'];
$setting = get_option( 'woocommerce_kshippingargentina-manager_settings' );
if ( ! isset( $setting['meta_dni'] ) || empty( $setting['meta_dni'] ) ) {
$fields['billing']['billing_vat_type'] = array(
'label' => __( 'Identification number type', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'select',
'required' => $required,
'priority' => $bp_country - 2,
'options' => WC_KShippingArgentina::$vat_types,
'class' => apply_filters( 'kshippingargentina_form_row_first_field', array( 'form-row-first', 'form-group', 'col-sm-12', 'col-md-6' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_first_input', array( 'form-control' ) ),
'clear' => true,
);
$fields['billing']['billing_vat'] = array(
'label' => __( 'Identification number', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $bp_country - 1,
'required' => $required,
'class' => apply_filters( 'kshippingargentina_form_row_last_field', array( 'form-row-last', 'form-group', 'col-sm-12', 'col-md-6' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_last_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_phone'] ) || empty( $setting['meta_phone'] ) ) {
$fields['billing']['billing_kphone_prefix'] = array(
'label' => __( 'Mobile Phone Area Code', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $bp_phone + 1,
'required' => $required,
'class' => apply_filters( 'kshippingargentina_form_row_first_field', array( 'form-row-first', 'form-group', 'col-sm-12', 'col-md-6' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_first_input', array( 'form-control' ) ),
'clear' => true,
);
$fields['billing']['billing_kphone'] = array(
'label' => __( 'Mobile Phone (No Area Code)', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $bp_phone + 2,
'required' => $required,
'class' => apply_filters( 'kshippingargentina_form_row_last_field', array( 'form-row-last', 'form-group', 'col-sm-12', 'col-md-6' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_last_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_number'] ) || empty( $setting['meta_number'] ) ) {
$fields['billing']['billing_number'] = array(
'label' => __( 'Height (Enter numbers only)', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $bp_address2 + 1,
'custom_attributes' => array( 'pattern' => '[0-9]{1,8}' ),
'maxlength' => 8,
'required' => $required,
'class' => apply_filters( 'kshippingargentina_form_row_field', array( 'form-group', 'col-sm-12', 'col-md-12' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_number_shipping'] ) || empty( $setting['meta_number_shipping'] ) ) {
$fields['shipping']['shipping_number'] = array(
'label' => __( 'Height (Enter numbers only)', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $sp_address2 + 1,
'custom_attributes' => array( 'pattern' => '[0-9]{1,8}' ),
'maxlength' => 8,
'required' => $required,
'class' => apply_filters( 'kshippingargentina_form_row_field', array( 'form-group', 'col-sm-12', 'col-md-12' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_floor'] ) || empty( $setting['meta_floor'] ) ) {
$fields['billing']['billing_floor'] = array(
'label' => __( 'Floor', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $bp_address2 + 3,
'custom_attributes' => array( 'pattern' => '[a-zA-Z0-9 ]{0,3}' ),
'maxlength' => 3,
'required' => false,
'class' => apply_filters( 'kshippingargentina_form_row_field', array( 'form-group', 'col-sm-12', 'col-md-12' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_number_shipping'] ) || empty( $setting['meta_number_shipping'] ) ) {
$fields['shipping']['shipping_floor'] = array(
'label' => __( 'Floor', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $sp_address2 + 3,
'required' => false,
'custom_attributes' => array( 'pattern' => '[a-zA-Z0-9 ]{0,3}' ),
'maxlength' => 3,
'class' => apply_filters( 'kshippingargentina_form_row_field', array( 'form-group', 'col-sm-12', 'col-md-12' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_apartment'] ) || empty( $setting['meta_apartment'] ) ) {
$fields['billing']['billing_apartment'] = array(
'label' => __( 'Apartment', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $bp_address2 + 4,
'custom_attributes' => array( 'pattern' => '[a-zA-Z0-9 ]{0,3}' ),
'maxlength' => 3,
'required' => false,
'class' => apply_filters( 'kshippingargentina_form_row_field', array( 'form-group', 'col-sm-12', 'col-md-12' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_input', array( 'form-control' ) ),
'clear' => true,
);
}
if ( ! isset( $setting['meta_apartment_shipping'] ) || empty( $setting['meta_apartment_shipping'] ) ) {
$fields['shipping']['shipping_apartment'] = array(
'label' => __( 'Apartment', 'carriers-of-argentina-for-woocommerce' ),
'type' => 'text',
'priority' => $sp_address2 + 4,
'custom_attributes' => array( 'pattern' => '[a-zA-Z0-9 ]{0,3}' ),
'maxlength' => 3,
'required' => false,
'class' => apply_filters( 'kshippingargentina_form_row_field', array( 'form-group', 'col-sm-12', 'col-md-12' ) ),
'input_class' => apply_filters( 'kshippingargentina_form_row_input', array( 'form-control' ) ),
'clear' => true,
);
}
return $fields;
},
5
);
add_filter(
'woocommerce_default_address_fields',
function ( $address_fields ) {
$address_fields['address_1']['label'] = __( 'Street', 'carriers-of-argentina-for-woocommerce' );
$address_fields['address_1']['placeholder'] = __( 'Street name', 'carriers-of-argentina-for-woocommerce' );
$address_fields['address_2']['label'] = __( 'Detail (Between-streets, etc)', 'carriers-of-argentina-for-woocommerce' );
$address_fields['address_2']['placeholder'] = __( 'Detail (Between-streets, etc)', 'carriers-of-argentina-for-woocommerce' );
return $address_fields;
}
);
/**
* Custom function that get the chosen shipping method details for a cart item.
*
* @param string $cart_item_key Item key.
*/
function kshippingargentina_cart_item_shipping_method( $cart_item_key = false ) {
if ( ! WC()->session ) {
WC()->session = new WC_Session_Handler();
WC()->session->init();
}
$chosen_shippings = WC()->session->get( 'chosen_shipping_methods' ); // The chosen shipping methods.
KShippingArgentina_API::debug( 'Chosen Shippings: ', array( $chosen_shippings ) );
foreach ( WC()->cart->get_shipping_packages() as $id => $package ) {
$chosen = $chosen_shippings[ $id ]; // The chosen shipping method.
KShippingArgentina_API::debug( 'Chosen package: ', array( $id, $chosen ) );
if ( WC()->session->__isset( 'shipping_for_package_' . $id ) ) {
$prices = WC()->session->get( 'shipping_for_package_' . $id );
KShippingArgentina_API::debug( 'Chosen price: ', array( $id, $chosen, $prices ) );
return $prices['rates'][ $chosen ];
}
}
}
// Save shipping method details in order line items (product) as custom order item meta data.
add_action(
'woocommerce_checkout_create_order_line_item',
function ( $item, $cart_item_key, $values, $order ) {
// Load shipping rate for this item.
$rate = kshippingargentina_cart_item_shipping_method( $cart_item_key );
if ( strstr( $rate->id, 'kshippingargentina' ) === false ) {
return;
}
$order->update_meta_data( 'kshippingargentina_type', $rate );
$order->update_meta_data( 'kshippingargentina_cost', $rate->cost );
$order->update_meta_data( 'kshippingargentina_instance_id', $rate->instance_id );
$order->update_meta_data( 'kshippingargentina_label', $rate->label );
KShippingArgentina_API::debug( 'Chosen instance: ', array( $rate->id, $rate->cost, $rate->instance_id, $rate->label ) );
},
10,
4
);
if ( ! function_exists( 'array_key_first' ) ) {
/**
* Array key first.
*
* @param array $arr Items.
*/
function array_key_first( $arr ) {
foreach ( $arr as $key => $unused ) {
return $key;
}
return null;
}
}
add_action( 'wp_enqueue_scripts', 'kshippingargentina_hook_js' );
add_action( 'admin_enqueue_scripts', 'kshippingargentina_hook_js' );
/**
* Hook js function.
*/
function kshippingargentina_hook_js() {
if ( is_admin() ) {
wp_enqueue_script( 'jquery-ui-datepicker' );
}
wp_enqueue_script( 'wc-kshippingargentina-js', plugins_url( 'kshippingargentina_script.js', __FILE__ ), array( 'jquery' ), WC_KShippingArgentina::VERSION, true );
wp_localize_script(
'wc-kshippingargentina-js',
'wc_kshippingargentina_context',
array(
'token' => wp_create_nonce( 'kshippingargentina_token' ),
'ajax_url' => WC_AJAX::get_endpoint( 'wc_kshippingargentina_ajax' ),
'home_url' => home_url(),
'messages' => array(
'days' => __( 'days', 'carriers-of-argentina-for-woocommerce' ),
'empty_cart' => __( 'Your shopping cart is empty', 'carriers-of-argentina-for-woocommerce' ),
'carrier_empty' => __( 'No carriers found', 'carriers-of-argentina-for-woocommerce' ),
'product_not_found' => __( 'Product not found', 'carriers-of-argentina-for-woocommerce' ),
'server_error' => __( 'Server error', 'carriers-of-argentina-for-woocommerce' ),
'server_loading' => __( 'Loading...', 'carriers-of-argentina-for-woocommerce' ),
'not_installed' => __( 'The plugin is not configured correctly...', 'carriers-of-argentina-for-woocommerce' ),
),
)
);
}
add_action( 'wc_ajax_wc_kshippingargentina_ajax', 'wc_kshippingargentina_ajax' );
/**
* Ajax function.
*/
function wc_kshippingargentina_ajax() {
if ( ! class_exists( 'KShippingArgentina_API' ) ) {
include_once 'includes/class-kshippingargentina-api.php';
}
KShippingArgentina_API::init();
WC_KShippingArgentina::get_instance();
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'kshippingargentina_token' ) ) {
KShippingArgentina_API::debug( 'invalid nonce wc_kshippingargentina_ajax', $_POST );
die( wp_json_encode( array() ) );
}
KShippingArgentina_API::debug( 'wc_kshippingargentina_ajax', $_POST );
$setting = get_option( 'woocommerce_kshippingargentina-manager_settings' );
if ( isset( $_POST['cmd'] ) ) {
switch ( $_POST['cmd'] ) {
case 'offices_sender':
$postcode = $setting['postcode'];
if ( isset( $_POST['postcode'] ) ) {
$postcode = sanitize_text_field( wp_unslash( $_POST['postcode'] ) );
}
$service_type = false;
if ( ! isset( $_POST['instance_id'] ) && ! isset( $_POST['service_type'] ) ) {
die( wp_json_encode( array() ) );
}
if ( isset( $_POST['service_type'] ) ) {
$service_type = sanitize_text_field( wp_unslash( $_POST['service_type'] ) );
} else {
$shipping = WC_KShippingArgentina_Shipping::get_instance( (int) $_POST['instance_id'] );
$service_type = $shipping->service_type;
}
$offices = KShippingArgentina_API::get_office( $service_type, $postcode, true );
die( wp_json_encode( $offices ) );
case 'offices_rcv':
if ( ! isset( $_POST['postcode'] ) ) {
die( wp_json_encode( array() ) );
}
if ( ! isset( $_POST['instance_id'] ) ) {
die( wp_json_encode( array() ) );
}
$shipping = WC_KShippingArgentina_Shipping::get_instance( (int) $_POST['instance_id'] );
$offices = KShippingArgentina_API::get_office( $shipping->service_type, sanitize_text_field( wp_unslash( $_POST['postcode'] ) ), null, true );
die( wp_json_encode( $offices ) );
case 'cities':
if ( ! isset( $_POST['state'] ) ) {
die( wp_json_encode( array() ) );
}
$cities = KShippingArgentina_API::get_cities( sanitize_text_field( wp_unslash( $_POST['state'] ) ) );
die( wp_json_encode( $cities ) );
}
}
die( '"invalid_cmd"' );
}
add_filter(
'woocommerce_default_address_fields',
function ( $address_fields ) {
// as you can see, no needs to specify a field group anymore.
$country_p = $address_fields['country']['priority'];
$address_fields['state']['priority'] = $country_p + 1;
$address_fields['city']['priority'] = $country_p + 2;
return $address_fields;
}
);
add_filter(
'woocommerce_localisation_address_formats',
function ( $formats ) {
$formats['AR'] = "{name}\n{company}\n{billing_vat}\n{address_1} {shipping_number}\n{shipping_floor} {shipping_apartment}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}\n{shipping_office}";
return $formats;
}
);
add_filter(
'woocommerce_order_formatted_billing_address',
function ( $fields, $order ) {
$setting = get_option( 'woocommerce_kshippingargentina-manager_settings' );
$fields['billing_vat'] = trim( $order->get_meta( '_billing_vat_type' ) . ' ' . $order->get_meta( '_billing_vat' ) );
$fields['shipping_number'] = $order->get_meta( '_billing_number' );
$fields['shipping_floor'] = $order->get_meta( '_billing_floor' );
$fields['shipping_apartment'] = $order->get_meta( '_billing_apartment' );
if ( empty( $fields['shipping_number'] ) && isset( $setting['meta_number'] ) && ! empty( $setting['meta_number'] ) ) {
$fields['shipping_number'] = $order->get_meta( $setting['meta_number'] );
if ( ! $fields['shipping_number'] ) {
$fields['shipping_number'] = $order->get_meta( '_' . $setting['meta_number'] );
}
}
if ( empty( $fields['shipping_floor'] ) && isset( $setting['meta_floor'] ) && ! empty( $setting['meta_floor'] ) ) {
$fields['shipping_floor'] = $order->get_meta( $setting['meta_floor'] );
if ( ! $fields['shipping_floor'] ) {
$fields['shipping_floor'] = $order->get_meta( '_' . $setting['meta_floor'] );
}
}
if ( empty( $fields['shipping_apartment'] ) && isset( $setting['meta_apartment'] ) && ! empty( $setting['meta_apartment'] ) ) {
$fields['shipping_apartment'] = $order->get_meta( $setting['meta_apartment'] );
if ( ! $fields['shipping_apartment'] ) {
$fields['shipping_apartment'] = $order->get_meta( '_' . $setting['meta_apartment'] );
}
}
$shipping = false;
$offices = $order->get_meta( '_office_kshippingargentina' );
$instance_id = $order->get_meta( 'kshippingargentina_instance_id', true );
if ( $instance_id ) {
$shipping = WC_KShippingArgentina_Shipping::get_instance( $instance_id );
}
if ( $shipping && $shipping->office && isset( $offices [ $instance_id ] ) ) {
$address = $order->get_address( 'billing' );
if ( ! isset( $address['state'] ) || empty( $address['state'] ) ) {
$address = $order->get_address( 'shipping' );
}
$countries_obj = new WC_Countries();
$states = $countries_obj->get_states( 'AR' );
$state = $address['state'];
$state_name = $state;
$office = $offices [ $instance_id ]['office'];
$office_name = isset( $offices [ $instance_id ]['office_name'] ) && ! empty( $offices [ $instance_id ]['office_name'] ) ? $offices [ $instance_id ]['office_name'] : false;
if ( isset( $states[ $state ] ) ) {
$state_name = $states[ $state ];
}
$fields['shipping_office'] = sprintf(
// translators: Office.
__( 'Office: %1$s', 'carriers-of-argentina-for-woocommerce' ),
$office . ' - ' . $state_name
);
$postcode = preg_replace(
'/[^0-9]/',
'',
$address['postcode']
);
if ( $office_name ) {
$fields['shipping_office'] = sprintf(
// translators: Office.
__( 'Office: %1$s', 'carriers-of-argentina-for-woocommerce' ),
$office_name . ' (' . $office . ') - ' . $state_name
);
}
} else {
$fields['shipping_office'] = '';
}
return $fields;
},
10,
2
);
add_filter(
'woocommerce_order_formatted_shipping_address',
function ( $fields, $order ) {
$setting = get_option( 'woocommerce_kshippingargentina-manager_settings' );
$fields['billing_vat'] = trim( $order->get_meta( '_billing_vat_type' ) . ' ' . $order->get_meta( '_billing_vat' ) );
$fields['shipping_number'] = $order->get_meta( '_shipping_number' );
$fields['shipping_floor'] = $order->get_meta( '_shipping_floor' );
$fields['shipping_apartment'] = $order->get_meta( '_shipping_apartment' );
if ( empty( $fields['shipping_number'] ) && isset( $setting['meta_number_shipping'] ) && ! empty( $setting['meta_number_shipping'] ) ) {
$fields['shipping_number'] = $order->get_meta( $setting['meta_number_shipping'] );
if ( ! $fields['shipping_number'] ) {
$fields['shipping_number'] = $order->get_meta( '_' . $setting['meta_number_shipping'] );
}
}
if ( empty( $fields['shipping_floor'] ) && isset( $setting['meta_floor_shipping'] ) && ! empty( $setting['meta_floor_shipping'] ) ) {
$fields['shipping_floor'] = $order->get_meta( $setting['meta_floor_shipping'] );
if ( ! $fields['shipping_floor'] ) {
$fields['shipping_floor'] = $order->get_meta( '_' . $setting['meta_floor_shipping'] );
}
}
if ( empty( $fields['shipping_apartment'] ) && isset( $setting['meta_apartment_shipping'] ) && ! empty( $setting['meta_apartment_shipping'] ) ) {
$fields['shipping_apartment'] = $order->get_meta( $setting['meta_apartment_shipping'] );
if ( ! $fields['shipping_apartment'] ) {
$fields['shipping_apartment'] = $order->get_meta( '_' . $setting['meta_apartment_shipping'] );
}
}
$shipping = false;
$offices = $order->get_meta( '_office_kshippingargentina' );
$instance_id = $order->get_meta( 'kshippingargentina_instance_id', true );
if ( $instance_id ) {
$shipping = WC_KShippingArgentina_Shipping::get_instance( $instance_id );
}
if ( $shipping && $shipping->office && isset( $offices [ $instance_id ] ) ) {
$address = $order->get_address( 'shipping' );
if ( ! isset( $address['state'] ) || empty( $address['state'] ) ) {
$address = $order->get_address( 'billing' );
}
$countries_obj = new WC_Countries();
$states = $countries_obj->get_states( 'AR' );
$state = $address['state'];
$state_name = $state;
$office = $offices [ $instance_id ]['office'];
$office_name = isset( $offices [ $instance_id ]['office_name'] ) && ! empty( $offices [ $instance_id ]['office_name'] ) ? $offices [ $instance_id ]['office_name'] : false;
if ( isset( $states[ $state ] ) ) {
$state_name = $states[ $state ];
}
$fields['shipping_office'] = sprintf(
// translators: Office.
__( 'Office: %1$s', 'carriers-of-argentina-for-woocommerce' ),
$office . ' - ' . $state_name
);
$postcode = preg_replace(
'/[^0-9]/',
'',
$address['postcode']
);
if ( $office_name ) {
$fields['shipping_office'] = sprintf(
// translators: Office.
__( 'Office: %1$s', 'carriers-of-argentina-for-woocommerce' ),
$office_name . ' (' . $office . ') - ' . $state_name
);
}
} else {
$fields['shipping_office'] = '';
}
return $fields;
},
10,
2
);
add_filter(
'woocommerce_formatted_address_replacements',
function ( $replacements, $address ) {
$replacements['{billing_vat}'] = isset( $address['billing_vat'] ) ? $address['billing_vat'] : '';
$replacements['{shipping_number}'] = isset( $address['shipping_number'] ) ? $address['shipping_number'] : '';
$replacements['{shipping_floor}'] = isset( $address['shipping_floor'] ) ? $address['shipping_floor'] : '';
$replacements['{shipping_apartment}'] = isset( $address['shipping_apartment'] ) ? $address['shipping_apartment'] : '';
$replacements['{shipping_office}'] = isset( $address['shipping_office'] ) ? $address['shipping_office'] : '';
return $replacements;
},
10,
2
);
function kshipping_custom_column_name( $columns ) {
$return = array();
foreach ( $columns as $key => $data ) {
$return[ $key ] = $data;
if ( 'order_number' === $key ) {
$return['shipping_name'] = __( 'Shipping', 'carriers-of-argentina-for-woocommerce' );
}
}
return $return;
}
add_filter(
'manage_woocommerce_page_wc-orders_columns',
'kshipping_custom_column_name',
20
);
add_filter(
'manage_edit-shop_order_columns',
'kshipping_custom_column_name',
20
);
function kshiping_custom_column_content( $column, $order ) {
if ( 'shipping_name' === $column ) {
if ( ! is_object( $order ) ) {
$order = wc_get_order( $order );
}
$instance_id = $order->get_meta( 'kshippingargentina_instance_id', true );
if ( $instance_id ) {
$shipping = WC_KShippingArgentina_Shipping::get_instance( $instance_id );
if ( $shipping && isset( $shipping->service_type ) && ! empty( $shipping->service_type ) ) {
echo '<img src="' . esc_url( plugin_dir_url( __FILE__ ) . 'img/' . $shipping->service_type . '.png' ) . '" height="12" class="wc-kshippingargentina-logo" /> ';
}
}
echo esc_html( $order->get_shipping_method() );
}
}
add_action(
'manage_shop_order_posts_custom_column',
'kshiping_custom_column_content',
10,
2
);
add_action(
'manage_woocommerce_page_wc-orders_custom_column',
'kshiping_custom_column_content',
10,
2
);
add_action(
'wp_enqueue_scripts',
function () {
if ( is_checkout() && ! is_wc_endpoint_url() ) {
wp_enqueue_style( 'kshippingargentina_style', plugin_dir_url( __FILE__ ) . 'kshippingargentina_style.css', array(), WC_KShippingArgentina::VERSION );
}
}
);