forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-ajax.php
1695 lines (1244 loc) · 48.8 KB
/
woocommerce-ajax.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
/**
* WooCommerce Ajax Handlers
*
* Handles AJAX requests via wp_ajax hook (both admin and front-end events)
*
* @author WooThemes
* @category Core
* @package WooCommerce/Functions/AJAX
* @version 2.0.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/** Frontend AJAX events **************************************************/
/**
* woocommerce_get_refreshed_fragments function.
*
* @access public
* @return void
*/
function woocommerce_get_refreshed_fragments() {
global $woocommerce;
header( 'Content-Type: application/json; charset=utf-8' );
// Get mini cart
ob_start();
woocommerce_mini_cart();
$mini_cart = ob_get_clean();
// Fragments and mini cart are returned
$data = array(
'fragments' => apply_filters( 'add_to_cart_fragments', array(
'div.widget_shopping_cart_content' => '<div class="widget_shopping_cart_content">' . $mini_cart . '</div>'
)
),
'cart_hash' => $woocommerce->cart->get_cart() ? md5( json_encode( $woocommerce->cart->get_cart() ) ) : ''
);
echo json_encode( $data );
die();
}
add_action( 'wp_ajax_nopriv_woocommerce_get_refreshed_fragments', 'woocommerce_get_refreshed_fragments' );
add_action( 'wp_ajax_woocommerce_get_refreshed_fragments', 'woocommerce_get_refreshed_fragments' );
/**
* AJAX apply coupon on checkout page
*
* @access public
* @return void
*/
function woocommerce_ajax_apply_coupon() {
global $woocommerce;
check_ajax_referer( 'apply-coupon', 'security' );
if ( ! empty( $_POST['coupon_code'] ) ) {
$woocommerce->cart->add_discount( sanitize_text_field( $_POST['coupon_code'] ) );
} else {
wc_add_error( WC_Coupon::get_generic_coupon_error( WC_Coupon::E_WC_COUPON_PLEASE_ENTER ) );
}
wc_print_messages();
die();
}
add_action('wp_ajax_woocommerce_apply_coupon', 'woocommerce_ajax_apply_coupon');
add_action('wp_ajax_nopriv_woocommerce_apply_coupon', 'woocommerce_ajax_apply_coupon');
/**
* AJAX update shipping method on cart page
*
* @access public
* @return void
*/
function woocommerce_ajax_update_shipping_method() {
check_ajax_referer( 'update-shipping-method', 'security' );
if ( ! defined('WOOCOMMERCE_CART') ) define( 'WOOCOMMERCE_CART', true );
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( isset( $_POST['shipping_method'] ) && is_array( $_POST['shipping_method'] ) )
foreach ( $_POST['shipping_method'] as $i => $value )
$chosen_shipping_methods[ $i ] = woocommerce_clean( $value );
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
WC()->cart->calculate_totals();
woocommerce_cart_totals();
die();
}
add_action('wp_ajax_woocommerce_update_shipping_method', 'woocommerce_ajax_update_shipping_method');
add_action('wp_ajax_nopriv_woocommerce_update_shipping_method', 'woocommerce_ajax_update_shipping_method');
/**
* AJAX update order review on checkout
*
* @access public
* @return void
*/
function woocommerce_ajax_update_order_review() {
check_ajax_referer( 'update-order-review', 'security' );
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) )
define( 'WOOCOMMERCE_CHECKOUT', true );
if ( sizeof( WC()->cart->get_cart() ) == 0 ) {
echo '<div class="woocommerce-error">' . __( 'Sorry, your session has expired.', 'woocommerce' ) . ' <a href="' . home_url() . '">' . __( 'Return to homepage →', 'woocommerce' ) . '</a></div>';
die();
}
do_action( 'woocommerce_checkout_update_order_review', $_POST['post_data'] );
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
if ( isset( $_POST['shipping_method'] ) && is_array( $_POST['shipping_method'] ) )
foreach ( $_POST['shipping_method'] as $i => $value )
$chosen_shipping_methods[ $i ] = woocommerce_clean( $value );
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
WC()->session->set( 'chosen_payment_method', empty( $_POST['payment_method'] ) ? '' : $_POST['payment_method'] );
if ( isset( $_POST['country'] ) )
WC()->customer->set_country( $_POST['country'] );
if ( isset( $_POST['state'] ) )
WC()->customer->set_state( $_POST['state'] );
if ( isset( $_POST['postcode'] ) )
WC()->customer->set_postcode( $_POST['postcode'] );
if ( isset( $_POST['city'] ) )
WC()->customer->set_city( $_POST['city'] );
if ( isset( $_POST['address'] ) )
WC()->customer->set_address( $_POST['address'] );
if ( isset( $_POST['address_2'] ) )
WC()->customer->set_address_2( $_POST['address_2'] );
if ( isset( $_POST['s_country'] ) )
WC()->customer->set_shipping_country( $_POST['s_country'] );
if ( isset( $_POST['s_state'] ) )
WC()->customer->set_shipping_state( $_POST['s_state'] );
if ( isset( $_POST['s_postcode'] ) )
WC()->customer->set_shipping_postcode( $_POST['s_postcode'] );
if ( isset( $_POST['s_city'] ) )
WC()->customer->set_shipping_city( $_POST['s_city'] );
if ( isset( $_POST['s_address'] ) )
WC()->customer->set_shipping_address( $_POST['s_address'] );
if ( isset( $_POST['s_address_2'] ) )
WC()->customer->set_shipping_address_2( $_POST['s_address_2'] );
WC()->cart->calculate_totals();
do_action( 'woocommerce_checkout_order_review' ); // Display review order table
die();
}
add_action('wp_ajax_woocommerce_update_order_review', 'woocommerce_ajax_update_order_review');
add_action('wp_ajax_nopriv_woocommerce_update_order_review', 'woocommerce_ajax_update_order_review');
/**
* AJAX add to cart
*
* @access public
* @return void
*/
function woocommerce_ajax_add_to_cart() {
global $woocommerce;
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) );
$quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] );
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
if ( $passed_validation && $woocommerce->cart->add_to_cart( $product_id, $quantity ) ) {
do_action( 'woocommerce_ajax_added_to_cart', $product_id );
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) {
woocommerce_add_to_cart_message( $product_id );
}
// Return fragments
woocommerce_get_refreshed_fragments();
} else {
header( 'Content-Type: application/json; charset=utf-8' );
// If there was an error adding to the cart, redirect to the product page to show any errors
$data = array(
'error' => true,
'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
);
echo json_encode( $data );
}
die();
}
add_action('wp_ajax_woocommerce_add_to_cart', 'woocommerce_ajax_add_to_cart');
add_action('wp_ajax_nopriv_woocommerce_add_to_cart', 'woocommerce_ajax_add_to_cart');
/**
* Process ajax checkout form
*
* @access public
* @return void
*/
function woocommerce_process_checkout() {
global $woocommerce;
if (!defined('WOOCOMMERCE_CHECKOUT')) define('WOOCOMMERCE_CHECKOUT', true);
$woocommerce_checkout = $woocommerce->checkout();
$woocommerce_checkout->process_checkout();
die(0);
}
add_action('wp_ajax_woocommerce-checkout', 'woocommerce_process_checkout');
add_action('wp_ajax_nopriv_woocommerce-checkout', 'woocommerce_process_checkout');
/** Admin AJAX events *****************************************************/
/**
* Feature a product from admin
*
* @access public
* @return void
*/
function woocommerce_feature_product() {
if ( ! is_admin() ) die;
if ( ! current_user_can('edit_products') ) wp_die( __( 'You do not have sufficient permissions to access this page.', 'woocommerce' ) );
if ( ! check_admin_referer('woocommerce-feature-product')) wp_die( __( 'You have taken too long. Please go back and retry.', 'woocommerce' ) );
$post_id = isset( $_GET['product_id'] ) && (int) $_GET['product_id'] ? (int) $_GET['product_id'] : '';
if (!$post_id) die;
$post = get_post($post_id);
if ( ! $post || $post->post_type !== 'product' ) die;
$featured = get_post_meta( $post->ID, '_featured', true );
if ( $featured == 'yes' )
update_post_meta($post->ID, '_featured', 'no');
else
update_post_meta($post->ID, '_featured', 'yes');
wp_safe_redirect( remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() ) );
}
add_action('wp_ajax_woocommerce-feature-product', 'woocommerce_feature_product');
/**
* Mark an order as complete
*
* @access public
* @return void
*/
function woocommerce_mark_order_complete() {
if ( !is_admin() ) die;
if ( !current_user_can('edit_shop_orders') ) wp_die( __( 'You do not have sufficient permissions to access this page.', 'woocommerce' ) );
if ( !check_admin_referer('woocommerce-mark-order-complete')) wp_die( __( 'You have taken too long. Please go back and retry.', 'woocommerce' ) );
$order_id = isset($_GET['order_id']) && (int) $_GET['order_id'] ? (int) $_GET['order_id'] : '';
if (!$order_id) die;
$order = new WC_Order( $order_id );
$order->update_status( 'completed' );
wp_safe_redirect( wp_get_referer() );
}
add_action('wp_ajax_woocommerce-mark-order-complete', 'woocommerce_mark_order_complete');
/**
* Mark an order as processing
*
* @access public
* @return void
*/
function woocommerce_mark_order_processing() {
if ( !is_admin() ) die;
if ( !current_user_can('edit_shop_orders') ) wp_die( __( 'You do not have sufficient permissions to access this page.', 'woocommerce' ) );
if ( !check_admin_referer('woocommerce-mark-order-processing')) wp_die( __( 'You have taken too long. Please go back and retry.', 'woocommerce' ) );
$order_id = isset($_GET['order_id']) && (int) $_GET['order_id'] ? (int) $_GET['order_id'] : '';
if (!$order_id) die;
$order = new WC_Order( $order_id );
$order->update_status( 'processing' );
wp_safe_redirect( wp_get_referer() );
}
add_action('wp_ajax_woocommerce-mark-order-processing', 'woocommerce_mark_order_processing');
/**
* Add a new attribute via ajax function
*
* @access public
* @return void
*/
function woocommerce_add_new_attribute() {
check_ajax_referer( 'add-attribute', 'security' );
header( 'Content-Type: application/json; charset=utf-8' );
$taxonomy = esc_attr( $_POST['taxonomy'] );
$term = stripslashes( $_POST['term'] );
if ( taxonomy_exists( $taxonomy ) ) {
$result = wp_insert_term( $term, $taxonomy );
if ( is_wp_error($result) ) {
echo json_encode(array(
'error' => $result->get_error_message()
));
} else {
echo json_encode(array(
'term_id' => $result['term_id'],
'name' => $term,
'slug' => sanitize_title( $term ),
));
}
}
die();
}
add_action('wp_ajax_woocommerce_add_new_attribute', 'woocommerce_add_new_attribute');
/**
* Delete variation via ajax function
*
* @access public
* @return void
*/
function woocommerce_remove_variation() {
check_ajax_referer( 'delete-variation', 'security' );
$variation_id = intval( $_POST['variation_id'] );
$variation = get_post($variation_id);
if ( $variation && $variation->post_type == "product_variation" )
wp_delete_post( $variation_id );
die();
}
add_action('wp_ajax_woocommerce_remove_variation', 'woocommerce_remove_variation');
/**
* Delete variations via ajax function
*
* @access public
* @return void
*/
function woocommerce_remove_variations() {
check_ajax_referer( 'delete-variations', 'security' );
$variation_ids = (array) $_POST['variation_ids'];
foreach ( $variation_ids as $variation_id ) {
$variation = get_post($variation_id);
if ( $variation && $variation->post_type == "product_variation" )
wp_delete_post( $variation_id );
}
die();
}
add_action('wp_ajax_woocommerce_remove_variations', 'woocommerce_remove_variations');
function woocommerce_save_attributes() {
global $woocommerce;
check_ajax_referer( 'save-attributes', 'security' );
// Get post data
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
// Save Attributes
$attributes = array();
if ( isset( $data['attribute_names'] ) ) {
$attribute_names = array_map( 'stripslashes', $data['attribute_names'] );
$attribute_values = $data['attribute_values'];
if ( isset( $data['attribute_visibility'] ) )
$attribute_visibility = $data['attribute_visibility'];
if ( isset( $data['attribute_variation'] ) )
$attribute_variation = $data['attribute_variation'];
$attribute_is_taxonomy = $data['attribute_is_taxonomy'];
$attribute_position = $data['attribute_position'];
$attribute_names_count = sizeof( $attribute_names );
for ( $i=0; $i < $attribute_names_count; $i++ ) {
if ( ! $attribute_names[ $i ] )
continue;
$is_visible = isset( $attribute_visibility[ $i ] ) ? 1 : 0;
$is_variation = isset( $attribute_variation[ $i ] ) ? 1 : 0;
$is_taxonomy = $attribute_is_taxonomy[ $i ] ? 1 : 0;
if ( $is_taxonomy ) {
if ( isset( $attribute_values[ $i ] ) ) {
// Select based attributes - Format values (posted values are slugs)
if ( is_array( $attribute_values[ $i ] ) ) {
$values = array_map( 'sanitize_title', $attribute_values[ $i ] );
// Text based attributes - Posted values are term names - don't change to slugs
} else {
$values = array_map( 'stripslashes', array_map( 'strip_tags', explode( '|', $attribute_values[ $i ] ) ) );
}
// Remove empty items in the array
$values = array_filter( $values, 'strlen' );
} else {
$values = array();
}
// Update post terms
if ( taxonomy_exists( $attribute_names[ $i ] ) )
wp_set_object_terms( $post_id, $values, $attribute_names[ $i ] );
if ( $values ) {
// Add attribute to array, but don't set values
$attributes[ sanitize_title( $attribute_names[ $i ] ) ] = array(
'name' => woocommerce_clean( $attribute_names[ $i ] ),
'value' => '',
'position' => $attribute_position[ $i ],
'is_visible' => $is_visible,
'is_variation' => $is_variation,
'is_taxonomy' => $is_taxonomy
);
}
} elseif ( isset( $attribute_values[ $i ] ) ) {
// Text based, separate by pipe
$values = implode( ' | ', array_map( 'woocommerce_clean', array_map( 'stripslashes', explode( '|', $attribute_values[ $i ] ) ) ) );
// Custom attribute - Add attribute to array and set the values
$attributes[ sanitize_title( $attribute_names[ $i ] ) ] = array(
'name' => woocommerce_clean( $attribute_names[ $i ] ),
'value' => $values,
'position' => $attribute_position[ $i ],
'is_visible' => $is_visible,
'is_variation' => $is_variation,
'is_taxonomy' => $is_taxonomy
);
}
}
}
if ( ! function_exists( 'attributes_cmp' ) ) {
function attributes_cmp( $a, $b ) {
if ( $a['position'] == $b['position'] ) return 0;
return ( $a['position'] < $b['position'] ) ? -1 : 1;
}
}
uasort( $attributes, 'attributes_cmp' );
update_post_meta( $post_id, '_product_attributes', $attributes );
die();
}
add_action('wp_ajax_woocommerce_save_attributes', 'woocommerce_save_attributes');
/**
* Add variation via ajax function
*
* @access public
* @return void
*/
function woocommerce_add_variation() {
global $woocommerce;
check_ajax_referer( 'add-variation', 'security' );
$post_id = intval( $_POST['post_id'] );
$loop = intval( $_POST['loop'] );
$variation = array(
'post_title' => 'Product #' . $post_id . ' Variation',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_parent' => $post_id,
'post_type' => 'product_variation'
);
$variation_id = wp_insert_post( $variation );
do_action( 'woocommerce_create_product_variation', $variation_id );
if ( $variation_id ) {
$variation_post_status = 'publish';
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
// Get attributes
$attributes = (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) );
// Get tax classes
$tax_classes = array_filter(array_map('trim', explode("\n", get_option('woocommerce_tax_classes'))));
$tax_class_options = array();
$tax_class_options['parent'] =__( 'Same as parent', 'woocommerce' );
$tax_class_options[''] = __( 'Standard', 'woocommerce' );
if ($tax_classes) foreach ( $tax_classes as $class )
$tax_class_options[sanitize_title($class)] = $class;
// Get parent data
$parent_data = array(
'id' => $post_id,
'attributes' => $attributes,
'tax_class_options' => $tax_class_options,
'sku' => get_post_meta( $post_id, '_sku', true ),
'weight' => get_post_meta( $post_id, '_weight', true ),
'length' => get_post_meta( $post_id, '_length', true ),
'width' => get_post_meta( $post_id, '_width', true ),
'height' => get_post_meta( $post_id, '_height', true ),
'tax_class' => get_post_meta( $post_id, '_tax_class', true )
);
if ( ! $parent_data['weight'] )
$parent_data['weight'] = '0.00';
if ( ! $parent_data['length'] )
$parent_data['length'] = '0';
if ( ! $parent_data['width'] )
$parent_data['width'] = '0';
if ( ! $parent_data['height'] )
$parent_data['height'] = '0';
$_tax_class = '';
$image_id = 0;
$variation = get_post( $variation_id ); // Get the variation object
include( 'includes/admin/post-types/meta-boxes/views/html-variation-admin.php' );
}
die();
}
add_action('wp_ajax_woocommerce_add_variation', 'woocommerce_add_variation');
/**
* Link all variations via ajax function
*
* @access public
* @return void
*/
function woocommerce_link_all_variations() {
global $woocommerce;
if ( ! defined( 'WC_MAX_LINKED_VARIATIONS' ) ) {
define( 'WC_MAX_LINKED_VARIATIONS', 49 );
}
check_ajax_referer( 'link-variations', 'security' );
@set_time_limit(0);
$post_id = intval( $_POST['post_id'] );
if ( ! $post_id ) die();
$variations = array();
$_product = get_product( $post_id, array( 'product_type' => 'variable' ) );
// Put variation attributes into an array
foreach ( $_product->get_attributes() as $attribute ) {
if ( ! $attribute['is_variation'] ) continue;
$attribute_field_name = 'attribute_' . sanitize_title( $attribute['name'] );
if ( $attribute['is_taxonomy'] ) {
$post_terms = wp_get_post_terms( $post_id, $attribute['name'] );
$options = array();
foreach ( $post_terms as $term ) {
$options[] = $term->slug;
}
} else {
$options = explode( '|', $attribute['value'] );
}
$options = array_map( 'sanitize_title', array_map( 'trim', $options ) );
$variations[ $attribute_field_name ] = $options;
}
// Quit out if none were found
if ( sizeof( $variations ) == 0 ) die();
// Get existing variations so we don't create duplicates
$available_variations = array();
foreach( $_product->get_children() as $child_id ) {
$child = $_product->get_child( $child_id );
if ( ! empty( $child->variation_id ) ) {
$available_variations[] = $child->get_variation_attributes();
}
}
// Created posts will all have the following data
$variation_post_data = array(
'post_title' => 'Product #' . $post_id . ' Variation',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_parent' => $post_id,
'post_type' => 'product_variation'
);
// Now find all combinations and create posts
if ( ! function_exists( 'array_cartesian' ) ) {
function array_cartesian( $input ) {
$result = array();
while ( list( $key, $values ) = each( $input ) ) {
// If a sub-array is empty, it doesn't affect the cartesian product
if ( empty( $values ) ) {
continue;
}
// Special case: seeding the product array with the values from the first sub-array
if ( empty( $result ) ) {
foreach ( $values as $value ) {
$result[] = array( $key => $value );
}
}
else {
// Second and subsequent input sub-arrays work like this:
// 1. In each existing array inside $product, add an item with
// key == $key and value == first item in input sub-array
// 2. Then, for each remaining item in current input sub-array,
// add a copy of each existing array inside $product with
// key == $key and value == first item in current input sub-array
// Store all items to be added to $product here; adding them on the spot
// inside the foreach will result in an infinite loop
$append = array();
foreach( $result as &$product ) {
// Do step 1 above. array_shift is not the most efficient, but it
// allows us to iterate over the rest of the items with a simple
// foreach, making the code short and familiar.
$product[ $key ] = array_shift( $values );
// $product is by reference (that's why the key we added above
// will appear in the end result), so make a copy of it here
$copy = $product;
// Do step 2 above.
foreach( $values as $item ) {
$copy[ $key ] = $item;
$append[] = $copy;
}
// Undo the side effecst of array_shift
array_unshift( $values, $product[ $key ] );
}
// Out of the foreach, we can add to $results now
$result = array_merge( $result, $append );
}
}
return $result;
}
}
$variation_ids = array();
$added = 0;
$possible_variations = array_cartesian( $variations );
foreach ( $possible_variations as $variation ) {
// Check if variation already exists
if ( in_array( $variation, $available_variations ) )
continue;
$variation_id = wp_insert_post( $variation_post_data );
$variation_ids[] = $variation_id;
foreach ( $variation as $key => $value ) {
update_post_meta( $variation_id, $key, $value );
}
$added++;
do_action( 'product_variation_linked', $variation_id );
if ( $added > WC_MAX_LINKED_VARIATIONS )
break;
}
wc_delete_product_transients( $post_id );
echo $added;
die();
}
add_action( 'wp_ajax_woocommerce_link_all_variations', 'woocommerce_link_all_variations' );
/**
* Delete download permissions via ajax function
*
* @access public
* @return void
*/
function woocommerce_revoke_access_to_download() {
check_ajax_referer( 'revoke-access', 'security' );
global $wpdb;
$download_id = $_POST['download_id'];
$product_id = intval( $_POST['product_id'] );
$order_id = intval( $_POST['order_id'] );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE order_id = %d AND product_id = %d AND download_id = %d;", $order_id, $product_id, $download_id ) );
die();
}
add_action('wp_ajax_woocommerce_revoke_access_to_download', 'woocommerce_revoke_access_to_download');
/**
* Grant download permissions via ajax function
*
* @access public
* @return void
*/
function woocommerce_grant_access_to_download() {
check_ajax_referer( 'grant-access', 'security' );
global $wpdb;
$wpdb->hide_errors();
$order_id = intval( $_POST['order_id'] );
$product_id = intval( $_POST['product_id'] );
$loop = intval( $_POST['loop'] );
$file_count = 0;
$order = new WC_Order( $order_id );
$product = get_product( $product_id );
if ( ! $order->billing_email )
die();
$file_paths = apply_filters( 'woocommerce_file_download_paths', get_post_meta( $product_id, '_file_paths', true ), $product_id, $order_id, null );
if ( $file_paths ) {
foreach ( $file_paths as $download_id => $file_path ) {
if ( $inserted_id = woocommerce_downloadable_file_permission( $download_id, $product_id, $order ) ) {
// insert complete - get inserted data
$download = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $inserted_id ) );
$loop ++;
$file_count ++;
include( 'includes/admin/post-types/meta-boxes/views/html-order-download-permission.php' );
}
}
}
die();
}
add_action('wp_ajax_woocommerce_grant_access_to_download', 'woocommerce_grant_access_to_download');
/**
* Get customer details via ajax
*
* @access public
* @return void
*/
function woocommerce_get_customer_details() {
global $woocommerce;
check_ajax_referer( 'get-customer-details', 'security' );
header( 'Content-Type: application/json; charset=utf-8' );
$user_id = (int) trim(stripslashes($_POST['user_id']));
$type_to_load = esc_attr(trim(stripslashes($_POST['type_to_load'])));
$customer_data = array(
$type_to_load . '_first_name' => get_user_meta( $user_id, $type_to_load . '_first_name', true ),
$type_to_load . '_last_name' => get_user_meta( $user_id, $type_to_load . '_last_name', true ),
$type_to_load . '_company' => get_user_meta( $user_id, $type_to_load . '_company', true ),
$type_to_load . '_address_1' => get_user_meta( $user_id, $type_to_load . '_address_1', true ),
$type_to_load . '_address_2' => get_user_meta( $user_id, $type_to_load . '_address_2', true ),
$type_to_load . '_city' => get_user_meta( $user_id, $type_to_load . '_city', true ),
$type_to_load . '_postcode' => get_user_meta( $user_id, $type_to_load . '_postcode', true ),
$type_to_load . '_country' => get_user_meta( $user_id, $type_to_load . '_country', true ),
$type_to_load . '_state' => get_user_meta( $user_id, $type_to_load . '_state', true ),
$type_to_load . '_email' => get_user_meta( $user_id, $type_to_load . '_email', true ),
$type_to_load . '_phone' => get_user_meta( $user_id, $type_to_load . '_phone', true ),
);
$customer_data = apply_filters( 'woocommerce_found_customer_details', $customer_data );
echo json_encode( $customer_data );
// Quit out
die();
}
add_action('wp_ajax_woocommerce_get_customer_details', 'woocommerce_get_customer_details');
/**
* Add order item via ajax
*
* @access public
* @return void
*/
function woocommerce_ajax_add_order_item() {
global $woocommerce, $wpdb;
check_ajax_referer( 'order-item', 'security' );
$item_to_add = sanitize_text_field( $_POST['item_to_add'] );
$order_id = absint( $_POST['order_id'] );
// Find the item
if ( ! is_numeric( $item_to_add ) )
die();
$post = get_post( $item_to_add );
if ( ! $post || ( $post->post_type !== 'product' && $post->post_type !== 'product_variation' ) )
die();
$_product = get_product( $post->ID );
$order = new WC_Order( $order_id );
$class = 'new_row';
// Set values
$item = array();
$item['product_id'] = $_product->id;
$item['variation_id'] = isset( $_product->variation_id ) ? $_product->variation_id : '';
$item['name'] = $_product->get_title();
$item['tax_class'] = $_product->get_tax_class();
$item['qty'] = 1;
$item['line_subtotal'] = number_format( (double) $_product->get_price_excluding_tax(), 2, '.', '' );
$item['line_subtotal_tax'] = '';
$item['line_total'] = number_format( (double) $_product->get_price_excluding_tax(), 2, '.', '' );
$item['line_tax'] = '';
// Add line item
$item_id = woocommerce_add_order_item( $order_id, array(
'order_item_name' => $item['name'],
'order_item_type' => 'line_item'
) );
// Add line item meta
if ( $item_id ) {
woocommerce_add_order_item_meta( $item_id, '_qty', $item['qty'] );
woocommerce_add_order_item_meta( $item_id, '_tax_class', $item['tax_class'] );
woocommerce_add_order_item_meta( $item_id, '_product_id', $item['product_id'] );
woocommerce_add_order_item_meta( $item_id, '_variation_id', $item['variation_id'] );
woocommerce_add_order_item_meta( $item_id, '_line_subtotal', $item['line_subtotal'] );
woocommerce_add_order_item_meta( $item_id, '_line_subtotal_tax', $item['line_subtotal_tax'] );
woocommerce_add_order_item_meta( $item_id, '_line_total', $item['line_total'] );
woocommerce_add_order_item_meta( $item_id, '_line_tax', $item['line_tax'] );
}
do_action( 'woocommerce_ajax_add_order_item_meta', $item_id, $item );
$item = apply_filters( 'woocommerce_ajax_order_item', $item, $item_id );
include( 'includes/admin/post-types/meta-boxes/views/html-order-item.php' );
// Quit out
die();
}
add_action('wp_ajax_woocommerce_add_order_item', 'woocommerce_ajax_add_order_item');
/**
* Add order fee via ajax
*
* @access public
* @return void
*/
function woocommerce_ajax_add_order_fee() {
global $woocommerce;
check_ajax_referer( 'order-item', 'security' );
$order_id = absint( $_POST['order_id'] );
$order = new WC_Order( $order_id );
// Add line item
$item_id = woocommerce_add_order_item( $order_id, array(
'order_item_name' => '',
'order_item_type' => 'fee'
) );
// Add line item meta
if ( $item_id ) {
woocommerce_add_order_item_meta( $item_id, '_tax_class', '' );
woocommerce_add_order_item_meta( $item_id, '_line_total', '' );
woocommerce_add_order_item_meta( $item_id, '_line_tax', '' );
}
include( 'includes/admin/post-types/meta-boxes/views/html-order-fee.php' );
// Quit out
die();
}
add_action('wp_ajax_woocommerce_add_order_fee', 'woocommerce_ajax_add_order_fee');
/**
* woocommerce_ajax_remove_order_item function.
*
* @access public
* @return void
*/
function woocommerce_ajax_remove_order_item() {
global $woocommerce, $wpdb;
check_ajax_referer( 'order-item', 'security' );
$order_item_ids = $_POST['order_item_ids'];
if ( sizeof( $order_item_ids ) > 0 ) {
foreach( $order_item_ids as $id ) {
woocommerce_delete_order_item( absint( $id ) );
}