1 <?php
2 3 4 5 6 7 8 9 10 11 12
13 class WC_Checkout {
14
15
16 public $posted;
17
18
19 public $checkout_fields;
20
21
22 public $must_create_account;
23
24
25 public $enable_signup;
26
27
28 private $shipping_method;
29
30
31 private $payment_method;
32
33
34 private $customer_id;
35
36 37 38 39
40 protected static $_instance = null;
41
42 43 44 45 46 47 48 49 50 51
52 public static function instance() {
53 if ( is_null( self::$_instance ) )
54 self::$_instance = new self();
55 return self::$_instance;
56 }
57
58 59 60 61 62
63 public function __clone() {
64 _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
65 }
66
67 68 69 70 71
72 public function __wakeup() {
73 _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
74 }
75
76 77 78 79 80 81
82 public function __construct () {
83 add_action( 'woocommerce_checkout_billing', array( $this,'checkout_form_billing' ) );
84 add_action( 'woocommerce_checkout_shipping', array( $this,'checkout_form_shipping' ) );
85
86 $this->enable_signup = get_option( 'woocommerce_enable_signup_and_login_from_checkout' ) == 'yes' ? true : false;
87 $this->enable_guest_checkout = get_option( 'woocommerce_enable_guest_checkout' ) == 'yes' ? true : false;
88 $this->must_create_account = $this->enable_guest_checkout || is_user_logged_in() ? false : true;
89
90
91 $this->checkout_fields['billing'] = WC()->countries->get_address_fields( $this->get_value('billing_country'), 'billing_' );
92 $this->checkout_fields['shipping'] = WC()->countries->get_address_fields( $this->get_value('shipping_country'), 'shipping_' );
93
94 if ( get_option( 'woocommerce_registration_generate_username' ) == 'no' ) {
95 $this->checkout_fields['account']['account_username'] = array(
96 'type' => 'text',
97 'label' => __( 'Account username', 'woocommerce' ),
98 'required' => true,
99 'placeholder' => _x( 'Username', 'placeholder', 'woocommerce' )
100 );
101 }
102
103 if ( get_option( 'woocommerce_registration_generate_password' ) == 'no' ) {
104 $this->checkout_fields['account']['account_password'] = array(
105 'type' => 'password',
106 'label' => __( 'Account password', 'woocommerce' ),
107 'required' => true,
108 'placeholder' => _x( 'Password', 'placeholder', 'woocommerce' )
109 );
110 }
111
112 $this->checkout_fields['order'] = array(
113 'order_comments' => array(
114 'type' => 'textarea',
115 'class' => array('notes'),
116 'label' => __( 'Order Notes', 'woocommerce' ),
117 'placeholder' => _x('Notes about your order, e.g. special notes for delivery.', 'placeholder', 'woocommerce')
118 )
119 );
120
121 $this->checkout_fields = apply_filters( 'woocommerce_checkout_fields', $this->checkout_fields );
122
123 do_action( 'woocommerce_checkout_init', $this );
124 }
125
126
127 128 129
130 public function check_cart_items() {
131
132 do_action('woocommerce_check_cart_items');
133 }
134
135
136 137 138 139 140 141
142 public function checkout_form_billing() {
143 wc_get_template( 'checkout/form-billing.php', array( 'checkout' => $this ) );
144 }
145
146
147 148 149 150 151 152
153 public function checkout_form_shipping() {
154 wc_get_template( 'checkout/form-shipping.php', array( 'checkout' => $this ) );
155 }
156
157
158 159 160 161 162 163
164 public function create_order() {
165 global $wpdb;
166
167
168 $order_id = apply_filters( 'woocommerce_create_order', null, $this );
169
170 if ( is_numeric( $order_id ) )
171 return $order_id;
172
173
174 $order_data = apply_filters( 'woocommerce_new_order_data', array(
175 'post_type' => 'shop_order',
176 'post_title' => sprintf( __( 'Order – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ),
177 'post_status' => 'publish',
178 'ping_status' => 'closed',
179 'post_excerpt' => isset( $this->posted['order_comments'] ) ? $this->posted['order_comments'] : '',
180 'post_author' => 1,
181 'post_password' => uniqid( 'order_' )
182 ) );
183
184
185 $create_new_order = true;
186
187 if ( WC()->session->order_awaiting_payment > 0 ) {
188
189 $order_id = absint( WC()->session->order_awaiting_payment );
190
191
192 $terms = wp_get_object_terms( $order_id, 'shop_order_status', array( 'fields' => 'slugs' ) );
193 $order_status = isset( $terms[0] ) ? $terms[0] : 'pending';
194
195
196 if ( get_post( $order_id ) && ( $order_status == 'pending' || $order_status == 'failed' ) ) {
197
198
199 $create_new_order = false;
200 $order_data['ID'] = $order_id;
201 wp_update_post( $order_data );
202
203
204 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d )", $order_id ) );
205
206 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order_id ) );
207
208
209 do_action( 'woocommerce_resume_order', $order_id );
210 }
211 }
212
213 if ( $create_new_order ) {
214 $order_id = wp_insert_post( $order_data, true );
215
216 if ( is_wp_error( $order_id ) )
217 throw new Exception( 'Error: Unable to create order. Please try again.' );
218 else
219 do_action( 'woocommerce_new_order', $order_id );
220 }
221
222
223 if ( $this->checkout_fields['billing'] )
224 foreach ( $this->checkout_fields['billing'] as $key => $field ) {
225 update_post_meta( $order_id, '_' . $key, $this->posted[ $key ] );
226
227 if ( $this->customer_id && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) )
228 update_user_meta( $this->customer_id, $key, $this->posted[ $key ] );
229 }
230
231 if ( $this->checkout_fields['shipping'] && WC()->cart->needs_shipping() ) {
232 foreach ( $this->checkout_fields['shipping'] as $key => $field ) {
233 $postvalue = false;
234
235 if ( $this->posted['ship_to_different_address'] == false ) {
236 if ( isset( $this->posted[ str_replace( 'shipping_', 'billing_', $key ) ] ) ) {
237 $postvalue = $this->posted[ str_replace( 'shipping_', 'billing_', $key ) ];
238 update_post_meta( $order_id, '_' . $key, $postvalue );
239 }
240 } else {
241 $postvalue = $this->posted[ $key ];
242 update_post_meta( $order_id, '_' . $key, $postvalue );
243 }
244
245
246 if ( $postvalue && $this->customer_id && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) )
247 update_user_meta( $this->customer_id, $key, $postvalue );
248 }
249 }
250
251
252 if ( $this->customer_id )
253 do_action( 'woocommerce_checkout_update_user_meta', $this->customer_id, $this->posted );
254
255
256 foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
257
258 $_product = $values['data'];
259
260
261 $item_id = wc_add_order_item( $order_id, array(
262 'order_item_name' => $_product->get_title(),
263 'order_item_type' => 'line_item'
264 ) );
265
266
267 if ( $item_id ) {
268 wc_add_order_item_meta( $item_id, '_qty', apply_filters( 'woocommerce_stock_amount', $values['quantity'] ) );
269 wc_add_order_item_meta( $item_id, '_tax_class', $_product->get_tax_class() );
270 wc_add_order_item_meta( $item_id, '_product_id', $values['product_id'] );
271 wc_add_order_item_meta( $item_id, '_variation_id', $values['variation_id'] );
272 wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $values['line_subtotal'] ) );
273 wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $values['line_total'] ) );
274 wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $values['line_tax'] ) );
275 wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $values['line_subtotal_tax'] ) );
276
277
278 if ( $values['variation'] && is_array( $values['variation'] ) ) {
279 foreach ( $values['variation'] as $key => $value ) {
280 $key = str_replace( 'attribute_', '', $key );
281 wc_add_order_item_meta( $item_id, $key, $value );
282 }
283 }
284
285
286 if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $values['quantity'] ) ) {
287 wc_add_order_item_meta( $item_id, apply_filters( 'woocommerce_backordered_item_meta_name', __( 'Backordered', 'woocommerce' ), $cart_item_key, $order_id ), $values['quantity'] - max( 0, $_product->get_total_stock() ) );
288 }
289
290
291 do_action( 'woocommerce_add_order_item_meta', $item_id, $values, $cart_item_key );
292 }
293 }
294
295
296 foreach ( WC()->cart->get_fees() as $fee_key => $fee ) {
297 $item_id = wc_add_order_item( $order_id, array(
298 'order_item_name' => $fee->name,
299 'order_item_type' => 'fee'
300 ) );
301
302 if ( $fee->taxable )
303 wc_add_order_item_meta( $item_id, '_tax_class', $fee->tax_class );
304 else
305 wc_add_order_item_meta( $item_id, '_tax_class', '0' );
306
307 wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $fee->amount ) );
308 wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $fee->tax ) );
309
310
311 do_action( 'woocommerce_add_order_fee_meta', $order_id, $item_id, $fee, $fee_key );
312 }
313
314
315 $packages = WC()->shipping->get_packages();
316
317 foreach ( $packages as $i => $package ) {
318 if ( isset( $package['rates'][ $this->shipping_methods[ $i ] ] ) ) {
319
320 $method = $package['rates'][ $this->shipping_methods[ $i ] ];
321
322 $item_id = wc_add_order_item( $order_id, array(
323 'order_item_name' => $method->label,
324 'order_item_type' => 'shipping'
325 ) );
326
327 if ( $item_id ) {
328 wc_add_order_item_meta( $item_id, 'method_id', $method->id );
329 wc_add_order_item_meta( $item_id, 'cost', wc_format_decimal( $method->cost ) );
330 do_action( 'woocommerce_add_shipping_order_item', $order_id, $item_id, $i );
331 }
332 }
333 }
334
335
336 foreach ( array_keys( WC()->cart->taxes + WC()->cart->shipping_taxes ) as $key ) {
337 $code = WC()->cart->tax->get_rate_code( $key );
338
339 if ( $code ) {
340 $item_id = wc_add_order_item( $order_id, array(
341 'order_item_name' => $code,
342 'order_item_type' => 'tax'
343 ) );
344
345
346 if ( $item_id ) {
347 wc_add_order_item_meta( $item_id, 'rate_id', $key );
348 wc_add_order_item_meta( $item_id, 'label', WC()->cart->tax->get_rate_label( $key ) );
349 wc_add_order_item_meta( $item_id, 'compound', absint( WC()->cart->tax->is_compound( $key ) ? 1 : 0 ) );
350 wc_add_order_item_meta( $item_id, 'tax_amount', wc_format_decimal( isset( WC()->cart->taxes[ $key ] ) ? WC()->cart->taxes[ $key ] : 0 ) );
351 wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_format_decimal( isset( WC()->cart->shipping_taxes[ $key ] ) ? WC()->cart->shipping_taxes[ $key ] : 0 ) );
352 }
353 }
354 }
355
356
357 if ( $applied_coupons = WC()->cart->get_coupons() ) {
358 foreach ( $applied_coupons as $code => $coupon ) {
359
360 $item_id = wc_add_order_item( $order_id, array(
361 'order_item_name' => $code,
362 'order_item_type' => 'coupon'
363 ) );
364
365
366 if ( $item_id ) {
367 wc_add_order_item_meta( $item_id, 'discount_amount', isset( WC()->cart->coupon_discount_amounts[ $code ] ) ? WC()->cart->coupon_discount_amounts[ $code ] : 0 );
368 }
369 }
370 }
371
372 if ( $this->payment_method ) {
373 update_post_meta( $order_id, '_payment_method', $this->payment_method->id );
374 update_post_meta( $order_id, '_payment_method_title', $this->payment_method->get_title() );
375 }
376 if ( empty( $this->posted['billing_email'] ) && is_user_logged_in() ) {
377 $current_user = wp_get_current_user();
378 update_post_meta( $order_id, '_billing_email', $current_user->user_email );
379 }
380 update_post_meta( $order_id, '_order_shipping', wc_format_decimal( WC()->cart->shipping_total ) );
381 update_post_meta( $order_id, '_order_discount', wc_format_decimal( WC()->cart->get_order_discount_total() ) );
382 update_post_meta( $order_id, '_cart_discount', wc_format_decimal( WC()->cart->get_cart_discount_total() ) );
383 update_post_meta( $order_id, '_order_tax', wc_format_decimal( WC()->cart->tax_total ) );
384 update_post_meta( $order_id, '_order_shipping_tax', wc_format_decimal( WC()->cart->shipping_tax_total ) );
385 update_post_meta( $order_id, '_order_total', wc_format_decimal( WC()->cart->total, get_option( 'woocommerce_price_num_decimals' ) ) );
386
387 update_post_meta( $order_id, '_order_key', 'wc_' . apply_filters('woocommerce_generate_order_key', uniqid('order_') ) );
388 update_post_meta( $order_id, '_customer_user', absint( $this->customer_id ) );
389 update_post_meta( $order_id, '_order_currency', get_woocommerce_currency() );
390 update_post_meta( $order_id, '_prices_include_tax', get_option( 'woocommerce_prices_include_tax' ) );
391 update_post_meta( $order_id, '_customer_ip_address', isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] );
392 update_post_meta( $order_id, '_customer_user_agent', isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '' );
393
394
395 do_action( 'woocommerce_checkout_update_order_meta', $order_id, $this->posted );
396
397
398 wp_set_object_terms( $order_id, 'pending', 'shop_order_status' );
399
400 return $order_id;
401 }
402
403 404 405 406 407 408
409 public function process_checkout() {
410 global $wpdb, $current_user;
411
412 wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' );
413
414 if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) )
415 define( 'WOOCOMMERCE_CHECKOUT', true );
416
417
418 @set_time_limit(0);
419
420 do_action( 'woocommerce_before_checkout_process' );
421
422 if ( sizeof( WC()->cart->get_cart() ) == 0 )
423 wc_add_notice( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to homepage</a>', 'woocommerce' ), home_url() ), 'error' );
424
425 do_action( 'woocommerce_checkout_process' );
426
427
428 $this->posted['terms'] = isset( $_POST['terms'] ) ? 1 : 0;
429 $this->posted['createaccount'] = isset( $_POST['createaccount'] ) ? 1 : 0;
430 $this->posted['payment_method'] = isset( $_POST['payment_method'] ) ? stripslashes( $_POST['payment_method'] ) : '';
431 $this->posted['shipping_method'] = isset( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : '';
432 $this->posted['ship_to_different_address'] = isset( $_POST['ship_to_different_address'] ) ? true : false;
433
434 if ( isset( $_POST['shiptobilling'] ) ) {
435 _deprecated_argument( 'WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. THe template files are out of date' );
436
437 $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true;
438 }
439
440
441 if ( WC()->cart->ship_to_billing_address_only() )
442 $this->posted['ship_to_different_address'] = false;
443
444
445 $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
446
447 if ( isset( $this->posted['shipping_method'] ) && is_array( $this->posted['shipping_method'] ) ) {
448 foreach ( $this->posted['shipping_method'] as $i => $value ) {
449 $chosen_shipping_methods[ $i ] = wc_clean( $value );
450 }
451 }
452
453 WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
454 WC()->session->set( 'chosen_payment_method', $this->posted['payment_method'] );
455
456
457 $skipped_shipping = false;
458
459
460 foreach ( $this->checkout_fields as $fieldset_key => $fieldset ) {
461
462
463 if ( $fieldset_key == 'shipping' && ( $this->posted['ship_to_different_address'] == false || ! WC()->cart->needs_shipping() ) ) {
464 $skipped_shipping = true;
465 continue;
466 }
467
468
469 if ( $fieldset_key == 'account' && ( is_user_logged_in() || ( $this->must_create_account == false && empty( $this->posted['createaccount'] ) ) ) ) {
470 continue;
471 }
472
473 foreach ( $fieldset as $key => $field ) {
474
475 if ( ! isset( $field['type'] ) )
476 $field['type'] = 'text';
477
478
479 switch ( $field['type'] ) {
480 case "checkbox" :
481 $this->posted[ $key ] = isset( $_POST[ $key ] ) ? 1 : 0;
482 break;
483 case "multiselect" :
484 $this->posted[ $key ] = isset( $_POST[ $key ] ) ? implode( ', ', array_map( 'wc_clean', $_POST[ $key ] ) ) : '';
485 break;
486 case "textarea" :
487 $this->posted[ $key ] = isset( $_POST[ $key ] ) ? wp_strip_all_tags( wp_check_invalid_utf8( stripslashes( $_POST[ $key ] ) ) ) : '';
488 break;
489 default :
490 $this->posted[ $key ] = isset( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : '';
491 break;
492 }
493
494
495 $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_' . sanitize_title( $field['type'] ) . '_field', $this->posted[ $key ] );
496 $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_field_' . $key, $this->posted[ $key ] );
497
498
499 if ( isset( $field['required'] ) && $field['required'] && empty( $this->posted[ $key ] ) )
500 wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is a required field.', 'woocommerce' ), 'error' );
501
502 if ( ! empty( $this->posted[ $key ] ) ) {
503
504
505 if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) {
506 foreach ( $field['validate'] as $rule ) {
507 switch ( $rule ) {
508 case 'postcode' :
509 $this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) );
510
511 if ( ! WC_Validation::is_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ) ) :
512 wc_add_notice( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ), 'error' );
513 else :
514 $this->posted[ $key ] = wc_format_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] );
515 endif;
516 break;
517 case 'phone' :
518 $this->posted[ $key ] = wc_format_phone_number( $this->posted[ $key ] );
519
520 if ( ! WC_Validation::is_phone( $this->posted[ $key ] ) )
521 wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ), 'error' );
522 break;
523 case 'email' :
524 $this->posted[ $key ] = strtolower( $this->posted[ $key ] );
525
526 if ( ! is_email( $this->posted[ $key ] ) )
527 wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ), 'error' );
528 break;
529 case 'state' :
530
531 $valid_states = WC()->countries->get_states( $_POST[ $fieldset_key . '_country' ] );
532 if ( $valid_states )
533 $valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) );
534
535
536 if ( isset( $valid_state_values[ strtolower( $this->posted[ $key ] ) ] ) )
537 $this->posted[ $key ] = $valid_state_values[ strtolower( $this->posted[ $key ] ) ];
538
539
540 if ( $valid_states && sizeof( $valid_states ) > 0 )
541 if ( ! in_array( $this->posted[ $key ], array_keys( $valid_states ) ) )
542 wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ), 'error' );
543 break;
544 }
545 }
546 }
547 }
548 }
549 }
550
551
552 if ( isset( $this->posted['billing_country'] ) )
553 WC()->customer->set_country( $this->posted['billing_country'] );
554 if ( isset( $this->posted['billing_state'] ) )
555 WC()->customer->set_state( $this->posted['billing_state'] );
556 if ( isset( $this->posted['billing_postcode'] ) )
557 WC()->customer->set_postcode( $this->posted['billing_postcode'] );
558
559
560 if ( ! $skipped_shipping ) {
561
562
563 if ( isset( $this->posted['shipping_country'] ) )
564 WC()->customer->set_shipping_country( $this->posted['shipping_country'] );
565 if ( isset( $this->posted['shipping_state'] ) )
566 WC()->customer->set_shipping_state( $this->posted['shipping_state'] );
567 if ( isset( $this->posted['shipping_postcode'] ) )
568 WC()->customer->set_shipping_postcode( $this->posted['shipping_postcode'] );
569
570 } else {
571
572
573 if ( isset( $this->posted['billing_country'] ) )
574 WC()->customer->set_shipping_country( $this->posted['billing_country'] );
575 if ( isset( $this->posted['billing_state'] ) )
576 WC()->customer->set_shipping_state( $this->posted['billing_state'] );
577 if ( isset( $this->posted['billing_postcode'] ) )
578 WC()->customer->set_shipping_postcode( $this->posted['billing_postcode'] );
579
580 }
581
582
583 WC()->cart->calculate_totals();
584
585
586 if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && empty( $this->posted['terms'] ) && wc_get_page_id( 'terms' ) > 0 )
587 wc_add_notice( __( 'You must accept our Terms & Conditions.', 'woocommerce' ), 'error' );
588
589 if ( WC()->cart->needs_shipping() ) {
590
591 if ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) )
592 wc_add_notice( sprintf( __( 'Unfortunately <strong>we do not ship to %s</strong>. Please enter an alternative shipping address.', 'woocommerce' ), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country() ), 'error' );
593
594
595 $packages = WC()->shipping->get_packages();
596 $this->shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
597
598 foreach ( $packages as $i => $package ) {
599 if ( ! isset( $package['rates'][ $this->shipping_methods[ $i ] ] ) ) {
600 wc_add_notice( __( 'Invalid shipping method.', 'woocommerce' ), 'error' );
601 $this->shipping_methods[ $i ] = '';
602 }
603 }
604 }
605
606 if ( WC()->cart->needs_payment() ) {
607
608
609 $available_gateways = WC()->payment_gateways->get_available_payment_gateways();
610
611 if ( ! isset( $available_gateways[ $this->posted['payment_method'] ] ) ) {
612 $this->payment_method = '';
613 wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' );
614 } else {
615 $this->payment_method = $available_gateways[ $this->posted['payment_method'] ];
616 $this->payment_method->validate_fields();
617 }
618 }
619
620
621 do_action( 'woocommerce_after_checkout_validation', $this->posted );
622
623 if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && wc_notice_count( 'error' ) == 0 ) {
624
625 try {
626
627
628 $this->customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() );
629
630 if ( ! is_user_logged_in() && ( $this->must_create_account || ! empty( $this->posted['createaccount'] ) ) ) {
631
632 $username = ! empty( $this->posted['account_username'] ) ? $this->posted['account_username'] : '';
633 $password = ! empty( $this->posted['account_password'] ) ? $this->posted['account_password'] : '';
634 $new_customer = wc_create_new_customer( $this->posted['billing_email'], $username, $password );
635
636 if ( is_wp_error( $new_customer ) )
637 throw new Exception( $new_customer->get_error_message() );
638
639 $this->customer_id = $new_customer;
640
641 wc_set_customer_auth_cookie( $this->customer_id );
642
643
644 WC()->session->set( 'reload_checkout', true );
645
646
647 if ( $this->posted['billing_first_name'] && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) {
648 $userdata = array(
649 'ID' => $this->customer_id,
650 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '',
651 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '',
652 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : ''
653 );
654 wp_update_user( apply_filters( 'woocommerce_checkout_customer_userdata', $userdata, $this ) );
655 }
656 }
657
658
659 $this->check_cart_items();
660
661
662 if ( wc_notice_count( 'error' ) > 0 )
663 throw new Exception();
664
665 $order_id = $this->create_order();
666
667 do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted );
668
669
670 if ( WC()->cart->needs_payment() ) {
671
672
673 WC()->session->order_awaiting_payment = $order_id;
674
675
676 $result = $available_gateways[ $this->posted['payment_method'] ]->process_payment( $order_id );
677
678
679 if ( $result['result'] == 'success' ) {
680
681 $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id );
682
683 if ( is_ajax() ) {
684 echo '<!--WC_START-->' . json_encode( $result ) . '<!--WC_END-->';
685 exit;
686 } else {
687 wp_redirect( $result['redirect'] );
688 exit;
689 }
690
691 }
692
693 } else {
694
695 if ( empty( $order ) )
696 $order = new WC_Order( $order_id );
697
698
699 $order->payment_complete();
700
701
702 WC()->cart->empty_cart();
703
704
705 $return_url = $order->get_checkout_order_received_url();
706
707
708 if ( is_ajax() ) {
709 echo '<!--WC_START-->' . json_encode(
710 array(
711 'result' => 'success',
712 'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order )
713 )
714 ) . '<!--WC_END-->';
715 exit;
716 } else {
717 wp_safe_redirect(
718 apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order )
719 );
720 exit;
721 }
722
723 }
724
725 } catch ( Exception $e ) {
726
727 if ( ! empty( $e ) )
728 wc_add_notice( $e->getMessage(), 'error' );
729
730 }
731
732 }
733
734
735 if ( is_ajax() ) {
736
737 ob_start();
738 wc_print_notices();
739 $messages = ob_get_clean();
740
741 echo '<!--WC_START-->' . json_encode(
742 array(
743 'result' => 'failure',
744 'messages' => $messages,
745 'refresh' => isset( WC()->session->refresh_totals ) ? 'true' : 'false',
746 'reload' => isset( WC()->session->reload_checkout ) ? 'true' : 'false'
747 )
748 ) . '<!--WC_END-->';
749
750 unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
751 exit;
752 }
753 }
754
755
756 757 758 759 760 761 762
763 public function get_value( $input ) {
764 if ( ! empty( $_POST[ $input ] ) ) {
765
766 return wc_clean( $_POST[ $input ] );
767
768 } else {
769
770 $value = apply_filters( 'woocommerce_checkout_get_value', null, $input );
771
772 if ( $value !== null )
773 return $value;
774
775 if ( is_user_logged_in() ) {
776
777 $current_user = wp_get_current_user();
778
779 if ( $meta = get_user_meta( $current_user->ID, $input, true ) )
780 return $meta;
781
782 if ( $input == "billing_email" )
783 return $current_user->user_email;
784 }
785
786 switch ( $input ) {
787 case "billing_country" :
788 return apply_filters( 'default_checkout_country', WC()->customer->get_country() ? WC()->customer->get_country() : WC()->countries->get_base_country(), 'billing' );
789 case "billing_state" :
790 return apply_filters( 'default_checkout_state', WC()->customer->has_calculated_shipping() ? WC()->customer->get_state() : '', 'billing' );
791 case "billing_postcode" :
792 return apply_filters( 'default_checkout_postcode', WC()->customer->get_postcode() ? WC()->customer->get_postcode() : '', 'billing' );
793 case "shipping_country" :
794 return apply_filters( 'default_checkout_country', WC()->customer->get_shipping_country() ? WC()->customer->get_shipping_country() : WC()->countries->get_base_country(), 'shipping' );
795 case "shipping_state" :
796 return apply_filters( 'default_checkout_state', WC()->customer->has_calculated_shipping() ? WC()->customer->get_shipping_state() : '', 'shipping' );
797 case "shipping_postcode" :
798 return apply_filters( 'default_checkout_postcode', WC()->customer->get_shipping_postcode() ? WC()->customer->get_shipping_postcode() : '', 'shipping' );
799 default :
800 return apply_filters( 'default_checkout_' . $input, null, $input );
801 }
802 }
803 }
804 }
805