forked from andresdouglas/EDD-Simple-Shipping
/
edd-simple-shipping.php
executable file
·1601 lines (1311 loc) · 48.9 KB
/
edd-simple-shipping.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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Easy Digital Downloads - Simple Shipping
Plugin URI: http://easydigitaldownloads.com/extension/simple-shipping
Description: Provides the ability to charge simple shipping fees for physical products in EDD
Version: 2.1.5
Author: Pippin Williamson
Author URI: http://pippinsplugins.com
Contributors: mordauk
Text Domain: edd-simple-shipping
Domain Path: languages
*/
class EDD_Simple_Shipping {
private static $instance;
/**
* Flag for domestic / international shipping
*
* @since 1.0
*
* @access private
*/
private $is_domestic = true;
/**
* Flag for whether Frontend Submissions is enabled
*
* @since 2.0
*
* @access private
*/
private $is_fes = true;
/**
* Get active object instance
*
* @since 1.0
*
* @access public
* @static
* @return object
*/
public static function get_instance() {
if ( ! self::$instance )
self::$instance = new EDD_Simple_Shipping();
return self::$instance;
}
/**
* Class constructor. Includes constants, includes and init method.
*
* @since 1.0
*
* @access public
* @return void
*/
public function __construct() {
define( 'EDD_SIMPLE_SHIPPING_VERSION', '2.1.5' );
$this->init();
}
/**
* Run action and filter hooks.
*
* @since 1.0
*
* @access private
* @return void
*/
private function init() {
if( ! class_exists( 'Easy_Digital_Downloads' ) )
return; // EDD not present
global $edd_options;
// internationalization
add_action( 'init', array( $this, 'textdomain' ) );
// Check for dependent plugins
add_action( 'plugins_loaded', array( $this, 'plugins_check' ) );
// register our license key settings
add_filter( 'edd_settings_general', array( $this, 'settings' ), 1 );
// Add the meta box fields to Download Configuration
add_action( 'edd_meta_box_fields', array( $this, 'metabox' ), 10 );
// Add our variable pricing shipping header
add_action( 'edd_download_price_table_head', array( $this, 'price_header' ), 700 );
// Add our variable pricing shipping options
add_action( 'edd_download_price_table_row', array( $this, 'price_row' ), 700, 3 );
// Add our meta fields to the EDD save routine
add_filter( 'edd_metabox_fields_save', array( $this, 'meta_fields_save' ) );
// Save shipping details on edit
add_action( 'edd_updated_edited_purchase', array( $this, 'save_payment' ) );
// Check shipping rate when billing/shipping fields are changed
add_action( 'wp_ajax_edd_get_shipping_rate', array( $this, 'ajax_shipping_rate' ) );
add_action( 'wp_ajax_nopriv_edd_get_shipping_rate', array( $this, 'ajax_shipping_rate' ) );
// Apply shipping costs to the checkout
add_action( 'init', array( $this, 'apply_shipping_fees' ) );
// Display the shipping address fields
add_action( 'edd_purchase_form_after_cc_form', array( $this, 'address_fields' ), 999 );
// Check for errors on checkout submission
add_action( 'edd_checkout_error_checks', array( $this, 'error_checks' ), 10, 2 );
// Store shipping info
add_filter( 'edd_purchase_data_before_gateway', array( $this, 'set_shipping_info' ), 10, 2 );
// Send shipping info to PayPal
add_filter( 'edd_paypal_redirect_args', array( $this, 'send_shipping_to_paypal' ), 10, 2 );
// Display the user's shipping info in the View Details popup
add_action( 'edd_view_order_details_billing_after', array( $this, 'show_shipping_details' ), 10 );
// Set payment as not shipped
add_action( 'edd_insert_payment', array( $this, 'set_as_not_shipped' ), 10, 2 );
// Add the shipped status column
add_filter( 'edd_payments_table_columns', array( $this, 'add_shipped_column' ) );
// Make our Shipped? column sortable
add_filter( 'edd_payments_table_sortable_columns', array( $this, 'add_sortable_column' ) );
// Sort the payment history by orders that have been shipped or not
add_filter( 'edd_get_payments_args', array( $this, 'sort_payments' ) );
// Display our Shipped? column value
add_filter( 'edd_payments_table_column', array( $this, 'display_shipped_column_value' ), 10, 3 );
// Add our Shipped? checkbox to the edit payment screen
add_action( 'edd_edit_payment_bottom', array( $this, 'edit_payment_option' ) );
// Modify the admin sales notice
add_filter( 'edd_sale_notification', array( $this, 'admin_sales_notice' ), 10, 3 );
// Add a new box to the export screen
add_action( 'edd_reports_tab_export_content_bottom', array( $this, 'show_export_options' ) );
add_action( 'edd_unshipped_orders_export', array( $this, 'do_export' ) );
add_filter( 'edd_payments_table_bulk_actions', array( $this, 'register_bulk_action' ) );
add_action( 'edd_payments_table_do_bulk_action', array( $this, 'process_bulk_actions' ), 10, 2 );
if( $this->is_fes ) {
/**
* Frontend Submissions actions
*/
add_action( 'fes-order-table-column-title', array( $this, 'shipped_column_header' ), 10 );
add_action( 'fes-order-table-column-value', array( $this, 'shipped_column_value' ), 10 );
add_action( 'edd_payment_receipt_after', array( $this, 'payment_receipt_after' ), 10, 2 );
add_action( 'edd_toggle_shipped_status', array( $this, 'frontend_toggle_shipped_status' ) );
add_action( 'fes_custom_post_button', array( $this, 'edd_fes_simple_shipping_field_button' ) );
add_action( 'fes_admin_field_edd_simple_shipping', array( $this, 'edd_fes_simple_shipping_admin_field' ), 10, 3 );
add_filter( 'fes_formbuilder_custom_field', array( $this, 'edd_fes_simple_shipping_formbuilder_is_custom_field' ), 10, 2 );
add_action( 'fes_submit_submission_form_bottom', array( $this, 'edd_fes_simple_shipping_save_custom_fields' ) );
add_action( 'fes_render_field_edd_simple_shipping', array( $this, 'edd_fes_simple_shipping_field' ), 10, 3 );
}
// auto updater
if( is_admin() ) {
if( class_exists( 'EDD_License' ) ) {
$license = new EDD_License( __FILE__, 'Simple Shipping', EDD_SIMPLE_SHIPPING_VERSION, 'Pippin Williamson', 'edd_simple_shipping_license_key' );
}
}
}
/**
* Load plugin text domain
*
* @since 1.0
*
* @access private
* @return void
*/
public static function textdomain() {
// Set filter for plugin's languages directory
$lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/';
$lang_dir = apply_filters( 'edd_simple_shipping_lang_directory', $lang_dir );
// Load the translations
load_plugin_textdomain( 'edd-simple-shipping', false, $lang_dir );
}
/**
* Determine if dependent plugins are loaded and set flags appropriately
*
* @since 2.0
*
* @access private
* @return void
*/
public static function plugins_check() {
if( class_exists( 'EDD_Front_End_Submissions' ) ) {
$this->is_fes = true;
}
}
/**
* Render the extra meta box fields
*
* @since 1.0
*
* @access private
* @return void
*/
public function metabox( $post_id = 0 ) {
global $edd_options;
$enabled = get_post_meta( $post_id, '_edd_enable_shipping', true );
$display = $enabled ? '' : 'style="display:none;"';
$domestic = get_post_meta( $post_id, '_edd_shipping_domestic', true );
$international = get_post_meta( $post_id, '_edd_shipping_international', true );
?>
<div id="edd_simple_shipping">
<script type="text/javascript">jQuery(document).ready(function($) {$('#edd_enable_shipping').on('click',function() {$('#edd_simple_shipping_fields,.edd_prices_shipping').toggle();});});</script>
<p><strong><?php _e( 'Shipping Options', 'edd-simple-shipping' ); ?></strong></p>
<p>
<label for="edd_enable_shipping">
<input type="checkbox" name="_edd_enable_shipping" id="edd_enable_shipping" value="1"<?php checked( 1, $enabled ); ?>/>
<?php printf( __( 'Enable shipping for this %s', 'edd-simple-shipping' ), edd_get_label_singular() ); ?>
</label>
</p>
<div id="edd_simple_shipping_fields" <?php echo $display; ?>>
<table>
<tr>
<td>
<label for="edd_shipping_domestic"><?php _e( 'Domestic Rate:', 'edd-simple-shipping' ); ?> </label>
</td>
<td>
<?php if( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
<span><?php echo edd_currency_filter( '' ); ?></span><input type="number" min="0" step="0.01" class="small-text" value="<?php esc_attr_e( $domestic ); ?>" id="edd_shipping_domestic" name="_edd_shipping_domestic"/>
<?php else : ?>
<input type="number" min="0" step="0.01" class="small-text" value="<?php esc_attr_e( $domestic ); ?>" id="edd_shipping_domestic" name="_edd_shipping_domestic"/><?php echo edd_currency_filter( '' ); ?>
<?php endif; ?>
</td>
<td>
<label for="edd_shipping_international"><?php _e( 'International Rate:', 'edd-simple-shipping' ); ?> </label>
</td>
<td>
<?php if( ! isset( $edd_options['currency_position'] ) || $edd_options['currency_position'] == 'before' ) : ?>
<span><?php echo edd_currency_filter( '' ); ?></span><input type="number" min="0" step="0.01" class="small-text" value="<?php esc_attr_e( $international ); ?>" id="edd_shipping_international" name="_edd_shipping_international"/>
<?php else : ?>
<input type="number" min="0" step="0.01" class="small-text" value="<?php esc_attr_e( $international ); ?>" id="edd_shipping_international" name="_edd_shipping_international"/><?php echo edd_currency_filter( '' ); ?>
<?php endif; ?>
</td>
</tr>
</table>
</div>
</div>
<?php
}
/**
*Add the table header cell for price shipping
*
* @since 1.0
*
* @access private
* @return void
*/
function price_header( $post_id = 0 ) {
$enabled = get_post_meta( $post_id, '_edd_enable_shipping', true );
$display = $enabled ? '' : 'style="display:none;"';
?>
<th class="edd_prices_shipping"<?php echo $display; ?>><?php _e( 'Shipping', 'edd-simple-shipping' ); ?></th>
<?php
}
/**
*Add the table cell for price shipping
*
* @since 1.0
*
* @access private
* @return void
*/
function price_row( $post_id = 0, $price_key = 0, $args = array() ) {
$enabled = get_post_meta( $post_id, '_edd_enable_shipping', true );
$display = $enabled ? '' : 'style="display:none;"';
$prices = edd_get_variable_prices( $post_id );
$shipping = isset( $prices[ $price_key ]['shipping'] );
?>
<td class="edd_prices_shipping"<?php echo $display; ?>>
<label for="edd_variable_prices[<?php echo $price_key; ?>][shipping]">
<input type="checkbox" value="1"<?php checked( true, $shipping ); ?> id="edd_variable_prices[<?php echo $price_key; ?>][shipping]" name="edd_variable_prices[<?php echo $price_key; ?>][shipping]" style="float:left;width:auto;margin:2px 5px 0 0;"/>
<span><?php _e( 'Check to enable shipping costs for this price.', 'edd-simple-shipping' ); ?></span>
</label>
</td>
<?php
}
/**
* Save our extra meta box fields
*
* @since 1.0
*
* @access private
* @return array
*/
public function meta_fields_save( $fields ) {
// Tell EDD to save our extra meta fields
$fields[] = '_edd_enable_shipping';
$fields[] = '_edd_shipping_domestic';
$fields[] = '_edd_shipping_international';
return $fields;
}
/**
* Save the shipping details on payment edit
*
* @since 1.5
*
* @access private
* @return void
*/
public function save_payment( $payment_id = 0 ) {
$address = isset( $_POST['edd-payment-shipping-address'] ) ? $_POST['edd-payment-shipping-address'] : false;
if( ! $address )
return;
$meta = edd_get_payment_meta( $payment_id );
$user_info = maybe_unserialize( $meta['user_info'] );
$user_info['shipping_info'] = $address[0];
$meta['user_info'] = $user_info;
update_post_meta( $payment_id, '_edd_payment_meta', $meta );
if( isset( $_POST['edd-payment-shipped'] ) ) {
update_post_meta( $payment_id, '_edd_payment_shipping_status', '2' );
} elseif( get_post_meta( $payment_id, '_edd_payment_shipping_status', true ) ) {
update_post_meta( $payment_id, '_edd_payment_shipping_status', '1' );
}
}
/**
* Determine if a product has snipping enabled
*
* @since 1.0
*
* @access private
* @return bool
*/
private function item_has_shipping( $item_id = 0, $price_id = 0 ) {
$enabled = get_post_meta( $item_id, '_edd_enable_shipping', true );
$variable_pricing = edd_has_variable_prices( $item_id );
if( $variable_pricing && ! $this->price_has_shipping( $item_id, $price_id ) )
$enabled = false;
return (bool) apply_filters( 'edd_simple_shipping_item_has_shipping', $enabled, $item_id );
}
/**
* Determine if a price option has snipping enabled
*
* @since 1.0
*
* @access private
* @return bool
*/
private function price_has_shipping( $item_id = 0, $price_id = 0 ) {
$prices = edd_get_variable_prices( $item_id );
$ret = isset( $prices[ $price_id ]['shipping'] );
return (bool) apply_filters( 'edd_simple_shipping_price_hasa_shipping', $ret, $item_id, $price_id );
}
/**
* Determine if shipping costs need to be calculated for the cart
*
* @since 1.0
*
* @access private
* @return bool
*/
private function cart_needs_shipping() {
$cart_contents = edd_get_cart_contents();
$ret = false;
if( is_array( $cart_contents ) ) {
foreach( $cart_contents as $item ) {
$price_id = isset( $item['options']['price_id'] ) ? (int) $item['options']['price_id'] : null;
if( $this->item_has_shipping( $item['id'], $price_id ) ) {
$ret = true;
break;
}
}
}
return (bool) apply_filters( 'edd_simple_shipping_cart_needs_shipping', $ret );
}
/**
* Get the base country (where the store is located)
*
* This is used for determining if customer should be charged domestic or international shipping
*
* @since 1.0
*
* @access private
* @return string
*/
private function get_base_region( $download_id = 0 ) {
global $edd_options;
if( ! empty( $download_id ) ) {
$author = get_post_field( 'post_author', $download_id );
$country = get_user_meta( $author, 'vendor_country', true );
if( $country ) {
$countries = edd_get_country_list();
$code = array_search( $country, $countries );
if( false !== $code ) {
$base_region = $code;
}
}
}
$base_region = isset( $base_region ) ? $base_region : edd_get_option( 'edd_simple_shipping_base_country', 'US' );
return $base_region;
}
/**
* Update the shipping costs via ajax
*
* This fires when the customer changes the country they are shipping to
*
* @since 1.0
*
* @access private
* @return void
*/
public function ajax_shipping_rate() {
// Calculate new shipping
$shipping = $this->apply_shipping_fees();
ob_start();
edd_checkout_cart();
$cart = ob_get_clean();
$response = array(
'html' => $cart,
'total' => html_entity_decode( edd_cart_total( false ), ENT_COMPAT, 'UTF-8' ),
);
echo json_encode( $response );
die();
}
/**
* Apply the shipping fees to the cart
*
* @since 1.0
*
* @access private
* @return void
*/
public function apply_shipping_fees() {
$this->remove_shipping_fees();
if( ! $this->cart_needs_shipping() ) {
return;
}
$cart_contents = edd_get_cart_contents();
if( ! is_array( $cart_contents ) ) {
return;
}
$amount = 0.00;
foreach( $cart_contents as $key => $item ) {
$price_id = isset( $item['options']['price_id'] ) ? (int) $item['options']['price_id'] : null;
if( ! $this->item_has_shipping( $item['id'], $price_id ) ) {
continue;
}
if( is_user_logged_in() && empty( $_POST['country'] ) ) {
$address = get_user_meta( get_current_user_id(), '_edd_user_address', true );
if( isset( $address['country'] ) && $address['country'] != $this->get_base_region( $item['id'] ) ) {
$this->is_domestic = false;
} else {
$this->is_domestic = true;
}
} else {
$country = ! empty( $_POST['country'] ) ? $_POST['country'] : $this->get_base_region();
if( $country != $this->get_base_region( $item['id'] ) ) {
$this->is_domestic = false;
} else {
$this->is_domestic = true;
}
}
if( $this->is_domestic ) {
$amount = (float) get_post_meta( $item['id'], '_edd_shipping_domestic', true );
} else {
$amount = (float) get_post_meta( $item['id'], '_edd_shipping_international', true );
}
if( $amount > 0 ) {
EDD()->fees->add_fee( array(
'amount' => $amount,
'label' => sprintf( __( '%s Shipping', 'edd-simple-shipping' ), get_the_title( $item['id'] ) ),
'id' => 'simple_shipping_' . $key,
'download_id' => $item['id']
) );
}
}
}
/**
* Removes all shipping fees from the cart
*
* @since 2.1
*
* @access public
* @return void
*/
public function remove_shipping_fees() {
$fees = EDD()->fees->get_fees( 'fee' );
if( empty( $fees ) ) {
return;
}
foreach( $fees as $key => $fee ) {
if( false === strpos( $key, 'simple_shipping' ) ) {
continue;
}
unset( $fees[ $key ] );
}
EDD()->session->set( 'edd_cart_fees', $fees );
}
/**
* Determine if the shipping fields should be displayed
*
* @since 1.0
*
* @access private
* @return bool
*/
private function needs_shipping_fields() {
return $this->cart_needs_shipping();
}
/**
* Determine if the current payment method has billing fields
*
* If no billing fields are present, the shipping fields are always displayed
*
* @since 1.0
*
* @access private
* @return bool
*/
private function has_billing_fields() {
$did_action = did_action( 'edd_after_cc_fields', 'edd_default_cc_address_fields' );
if( ! $did_action && edd_use_taxes() )
$did_action = did_action( 'edd_purchase_form_after_cc_form', 'edd_checkout_tax_fields' );
// Have to assume all gateways are using the default CC fields (they should be)
return ( $did_action || isset( $_POST['card_address'] ) );
}
/**
* Shipping info fields
*
* @since 1.0
*
* @access public
* @return void
*/
public function address_fields() {
if( ! $this->needs_shipping_fields() )
return;
$display = $this->has_billing_fields() ? ' style="display:none;"' : '';
ob_start();
?>
<script type="text/javascript">var edd_global_vars; jQuery(document).ready(function($) {
$('body').on('change', 'select[name=shipping_country],select[name=billing_country]',function() {
var billing = true;
if( $('select[name=billing_country]').length && ! $('#edd_simple_shipping_show').is(':checked') ) {
var val = $('select[name=billing_country]').val();
} else {
var val = $('select[name=shipping_country]').val();
billing = false;
}
if( billing && edd_global_vars.taxes_enabled == '1' )
return; // EDD core will recalculate on billing address change if taxes are enabled
if( val == 'US' ) {
$('#shipping_state_other').hide();$('#shipping_state_us').show();$('#shipping_state_ca').hide();
} else if( val =='CA') {
$('#shipping_state_other').hide();$('#shipping_state_us').hide();$('#shipping_state_ca').show();
} else {
$('#shipping_state_other').show();$('#shipping_state_us').hide();$('#shipping_state_ca').hide();
}
var postData = {
action: 'edd_get_shipping_rate',
country: val
};
$.ajax({
type: "POST",
data: postData,
dataType: "json",
url: edd_global_vars.ajaxurl,
success: function (response) {
$('#edd_checkout_cart').replaceWith(response.html);
$('.edd_cart_amount').each(function() {
$(this).text(response.total);
});
}
}).fail(function (data) {
if ( window.console && window.console.log ) {
console.log( data );
}
});
});
$('body').on('edd_taxes_recalculated', function( event, data ) {
if( $('#edd_simple_shipping_show').is(':checked') )
return;
var postData = {
action: 'edd_get_shipping_rate',
country: data.postdata.billing_country,
state: data.postdata.state
};
$.ajax({
type: "POST",
data: postData,
dataType: "json",
url: edd_global_vars.ajaxurl,
success: function (response) {
if( response ) {
$('#edd_checkout_cart').replaceWith(response.html);
$('.edd_cart_amount').each(function() {
$(this).text(response.total);
});
} else {
if ( window.console && window.console.log ) {
console.log( response );
}
}
}
}).fail(function (data) {
if ( window.console && window.console.log ) {
console.log( data );
}
});
});
$('select#edd-gateway, input.edd-gateway').change( function (e) {
var postData = {
action: 'edd_get_shipping_rate',
country: 'US' // default
};
$.ajax({
type: "POST",
data: postData,
dataType: "json",
url: edd_global_vars.ajaxurl,
success: function (response) {
$('#edd_checkout_cart').replaceWith(response.html);
$('.edd_cart_amount').each(function() {
$(this).text(response.total);
});
}
}).fail(function (data) {
if ( window.console && window.console.log ) {
console.log( data );
}
});
});
$('#edd_simple_shipping_show').change(function() {
$('#edd_simple_shipping_fields_wrap').toggle();
});
});</script>
<div id="edd_simple_shipping">
<?php if( $this->has_billing_fields() ) : ?>
<fieldset id="edd_simple_shipping_diff_address">
<label for="edd_simple_shipping_show">
<input type="checkbox" id="edd_simple_shipping_show" name="edd_use_different_shipping" value="1"/>
<?php _e( 'Ship to Different Address?', 'edd-simple-shipping' ); ?>
</label>
</fieldset>
<?php endif; ?>
<div id="edd_simple_shipping_fields_wrap"<?php echo $display; ?>>
<fieldset id="edd_simple_shipping_fields">
<?php do_action( 'edd_shipping_address_top' ); ?>
<legend><?php _e( 'Shipping Details', 'edd-simple-shipping' ); ?></legend>
<p id="edd-shipping-address-wrap">
<label class="edd-label"><?php _e( 'Shipping Address', 'edd-simple-shipping' ); ?></label>
<span class="edd-description"><?php _e( 'The address to ship your purchase to.', 'edd-simple-shipping' ); ?></span>
<input type="text" name="shipping_address" class="shipping-address edd-input" placeholder="<?php _e( 'Address line 1', 'edd-simple-shipping' ); ?>"/>
</p>
<p id="edd-shipping-address-2-wrap">
<label class="edd-label"><?php _e( 'Shipping Address Line 2', 'edd-simple-shipping' ); ?></label>
<span class="edd-description"><?php _e( 'The suite, apt no, PO box, etc, associated with your shipping address.', 'edd-simple-shipping' ); ?></span>
<input type="text" name="shipping_address_2" class="shipping-address-2 edd-input" placeholder="<?php _e( 'Address line 2', 'edd-simple-shipping' ); ?>"/>
</p>
<p id="edd-shipping-city-wrap">
<label class="edd-label"><?php _e( 'Shipping City', 'edd-simple-shipping' ); ?></label>
<span class="edd-description"><?php _e( 'The city for your shipping address.', 'edd-simple-shipping' ); ?></span>
<input type="text" name="shipping_city" class="shipping-city edd-input" placeholder="<?php _e( 'City', 'edd-simple-shipping' ); ?>"/>
</p>
<p id="edd-shipping-country-wrap">
<label class="edd-label"><?php _e( 'Shipping Country', 'edd-simple-shipping' ); ?></label>
<span class="edd-description"><?php _e( 'The country for your shipping address.', 'edd-simple-shipping' ); ?></span>
<select name="shipping_country" class="shipping-country edd-select">
<?php
$countries = edd_get_country_list();
foreach( $countries as $country_code => $country ) {
echo '<option value="' . $country_code . '">' . $country . '</option>';
}
?>
</select>
</p>
<p id="edd-shipping-state-wrap">
<label class="edd-label"><?php _e( 'Shipping State / Province', 'edd-simple-shipping' ); ?></label>
<span class="edd-description"><?php _e( 'The state / province for your shipping address.', 'edd-simple-shipping' ); ?></span>
<input type="text" size="6" name="shipping_state_other" id="shipping_state_other" class="shipping-state edd-input" placeholder="<?php _e( 'State / Province', 'edd-simple-shipping' ); ?>" style="display:none;"/>
<select name="shipping_state_us" id="shipping_state_us" class="shipping-state edd-select">
<?php
$states = edd_get_states_list();
foreach( $states as $state_code => $state ) {
echo '<option value="' . $state_code . '">' . $state . '</option>';
}
?>
</select>
<select name="shipping_state_ca" id="shipping_state_ca" class="shipping-state edd-select" style="display: none;">
<?php
$provinces = edd_get_provinces_list();
foreach( $provinces as $province_code => $province ) {
echo '<option value="' . $province_code . '">' . $province . '</option>';
}
?>
</select>
</p>
<p id="edd-shipping-zip-wrap">
<label class="edd-label"><?php _e( 'Shipping Zip / Postal Code', 'edd-simple-shipping' ); ?></label>
<span class="edd-description"><?php _e( 'The zip / postal code for your shipping address.', 'edd-simple-shipping' ); ?></span>
<input type="text" size="4" name="shipping_zip" class="shipping-zip edd-input" placeholder="<?php _e( 'Zip / Postal code', 'edd-simple-shipping' ); ?>"/>
</p>
<?php do_action( 'edd_shipping_address_bottom' ); ?>
</fieldset>
</div>
</div>
<?php echo ob_get_clean();
}
/**
* Perform error checks during checkout
*
* @since 1.0
*
* @access public
* @return void
*/
public function error_checks( $valid_data, $post_data ) {
// Only perform error checks if we have a product that needs shipping
if( ! $this->cart_needs_shipping() )
return;
// Check to see if shipping is different than billing
if( isset( $post_data['edd_use_different_shipping'] ) || ! $this->has_billing_fields() ) {
// Shipping address is different
if( empty( $post_data['shipping_address'] ) )
edd_set_error( 'missing_address', __( 'Please enter a shipping address', 'edd-simple-shipping' ) );
if( empty( $post_data['shipping_city'] ) )
edd_set_error( 'missing_city', __( 'Please enter a city for shipping', 'edd-simple-shipping' ) );
if( empty( $post_data['shipping_zip'] ) )
edd_set_error( 'missing_zip', __( 'Please enter a zip/postal code for shipping', 'edd-simple-shipping' ) );
} else {
// Shipping address is the same as billing
if( empty( $post_data['card_address'] ) )
edd_set_error( 'missing_address', __( 'Please enter a shipping address', 'edd-simple-shipping' ) );
if( empty( $post_data['card_city'] ) )
edd_set_error( 'missing_city', __( 'Please enter a city for shipping', 'edd-simple-shipping' ) );
if( empty( $post_data['card_zip'] ) )
edd_set_error( 'missing_zip', __( 'Please enter a zip/postal code for shipping', 'edd-simple-shipping' ) );
}
}
/**
* Attach our shipping info to the payment gateway daya
*
* @since 1.0
*
* @access public
* @return array
*/
public function set_shipping_info( $purchase_data, $valid_data ) {
if( ! $this->cart_needs_shipping() )
return $purchase_data;
$shipping_info = array();
// Check to see if shipping is different than billing
if( isset( $_POST['edd_use_different_shipping'] ) || ! $this->has_billing_fields() ) {
$shipping_info['address'] = sanitize_text_field( $_POST['shipping_address'] );
$shipping_info['address2'] = sanitize_text_field( $_POST['shipping_address_2'] );
$shipping_info['city'] = sanitize_text_field( $_POST['shipping_city'] );
$shipping_info['zip'] = sanitize_text_field( $_POST['shipping_zip'] );
$shipping_info['country'] = sanitize_text_field( $_POST['shipping_country'] );
// Shipping address is different
switch ( $_POST['shipping_country'] ) :
case 'US' :
$shipping_info['state'] = isset( $_POST['shipping_state_us'] ) ? sanitize_text_field( $_POST['shipping_state_us'] ) : '';
break;
case 'CA' :
$shipping_info['state'] = isset( $_POST['shipping_state_ca'] ) ? sanitize_text_field( $_POST['shipping_state_ca'] ) : '';
break;
default :
$shipping_info['state'] = isset( $_POST['shipping_state_other'] ) ? sanitize_text_field( $_POST['shipping_state_other'] ) : '';
break;
endswitch;
} else {
$shipping_info['address'] = sanitize_text_field( $_POST['card_address'] );
$shipping_info['address2'] = sanitize_text_field( $_POST['card_address_2'] );
$shipping_info['city'] = sanitize_text_field( $_POST['card_city'] );
$shipping_info['zip'] = sanitize_text_field( $_POST['card_zip'] );
$shipping_info['state'] = sanitize_text_field( $_POST['card_state'] );
$shipping_info['country'] = sanitize_text_field( $_POST['billing_country'] );
}
$purchase_data['user_info']['shipping_info'] = $shipping_info;
return $purchase_data;
}
/**
* Sets up the shipping details for PayPal
*
* This makes it possible to use the Print Shipping Label feature in PayPal
*
* @since 1.1
*
* @access public
* @return array
*/
public function send_shipping_to_paypal( $paypal_args = array(), $purchase_data = array() ) {
if( ! $this->cart_needs_shipping() )
return $paypal_args;
$shipping_info = $purchase_data['user_info']['shipping_info'];
$paypal_args['no_shipping'] = '0';
$paypal_args['address1'] = $shipping_info['address'];
$paypal_args['address2'] = $shipping_info['address2'];
$paypal_args['city'] = $shipping_info['city'];
$paypal_args['state'] = $shipping_info['country'] == 'US' ? $shipping_info['state'] : null;
$paypal_args['country'] = $shipping_info['country'];
$paypal_args['zip'] = $shipping_info['zip'];
return $paypal_args;
}
/**
* Set a purchase as not shipped
*
* This is so that we can grab all purchases in need of being shipped
*
* @since 1.0
*
* @access public
* @return void
*/
public function set_as_not_shipped( $payment_id = 0, $payment_data = array() ) {
$shipping_info = ! empty( $payment_data['user_info']['shipping_info'] ) ? $payment_data['user_info']['shipping_info'] : false;
if( ! $shipping_info )
return;
// Indicate that this purchase needs shipped
update_post_meta( $payment_id, '_edd_payment_shipping_status', '1' );
}
/**
* Display shipping details in the View Details popup
*
* @since 1.0
*
* @access public
* @return void
*/