| Conditions | 109 |
| Paths | > 20000 |
| Total Lines | 383 |
| Code Lines | 210 |
| Lines | 12 |
| Ratio | 3.13 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 374 | public function process_checkout() { |
||
| 375 | try { |
||
| 376 | if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'woocommerce-process_checkout' ) ) { |
||
| 377 | WC()->session->set( 'refresh_totals', true ); |
||
| 378 | throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) ); |
||
| 379 | } |
||
| 380 | |||
| 381 | if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
||
| 382 | define( 'WOOCOMMERCE_CHECKOUT', true ); |
||
| 383 | } |
||
| 384 | |||
| 385 | // Prevent timeout |
||
| 386 | @set_time_limit( 0 ); |
||
| 387 | |||
| 388 | do_action( 'woocommerce_before_checkout_process' ); |
||
| 389 | |||
| 390 | if ( WC()->cart->is_empty() ) { |
||
| 391 | throw new Exception( sprintf( __( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'shop' ) ) ) ); |
||
| 392 | } |
||
| 393 | |||
| 394 | do_action( 'woocommerce_checkout_process' ); |
||
| 395 | |||
| 396 | // Checkout fields (not defined in checkout_fields) |
||
| 397 | $this->posted['terms'] = isset( $_POST['terms'] ) ? 1 : 0; |
||
| 398 | $this->posted['createaccount'] = isset( $_POST['createaccount'] ) && ! empty( $_POST['createaccount'] ) ? 1 : 0; |
||
| 399 | $this->posted['payment_method'] = isset( $_POST['payment_method'] ) ? stripslashes( $_POST['payment_method'] ) : ''; |
||
| 400 | $this->posted['shipping_method'] = isset( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : ''; |
||
| 401 | $this->posted['ship_to_different_address'] = ! empty( $_POST['ship_to_different_address'] ); |
||
| 402 | |||
| 403 | if ( isset( $_POST['shiptobilling'] ) ) { |
||
| 404 | _deprecated_argument( 'WC_Checkout::process_checkout()', '2.1', 'The "shiptobilling" field is deprecated. The template files are out of date' ); |
||
| 405 | |||
| 406 | $this->posted['ship_to_different_address'] = $_POST['shiptobilling'] ? false : true; |
||
| 407 | } |
||
| 408 | |||
| 409 | // Ship to billing only option |
||
| 410 | if ( wc_ship_to_billing_address_only() ) { |
||
| 411 | $this->posted['ship_to_different_address'] = false; |
||
| 412 | } |
||
| 413 | |||
| 414 | // Update customer shipping and payment method to posted method |
||
| 415 | $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
||
| 416 | |||
| 417 | if ( isset( $this->posted['shipping_method'] ) && is_array( $this->posted['shipping_method'] ) ) { |
||
| 418 | foreach ( $this->posted['shipping_method'] as $i => $value ) { |
||
| 419 | $chosen_shipping_methods[ $i ] = wc_clean( $value ); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); |
||
| 424 | WC()->session->set( 'chosen_payment_method', $this->posted['payment_method'] ); |
||
| 425 | |||
| 426 | // Note if we skip shipping |
||
| 427 | $skipped_shipping = false; |
||
| 428 | |||
| 429 | // Get posted checkout_fields and do validation |
||
| 430 | foreach ( $this->checkout_fields as $fieldset_key => $fieldset ) { |
||
| 431 | |||
| 432 | // Skip shipping if not needed |
||
| 433 | if ( 'shipping' === $fieldset_key && ( false == $this->posted['ship_to_different_address'] || ! WC()->cart->needs_shipping_address() ) ) { |
||
| 434 | $skipped_shipping = true; |
||
| 435 | continue; |
||
| 436 | } |
||
| 437 | |||
| 438 | // Skip account if not needed |
||
| 439 | if ( 'account' === $fieldset_key && ( is_user_logged_in() || ( false === $this->must_create_account && empty( $this->posted['createaccount'] ) ) ) ) { |
||
| 440 | continue; |
||
| 441 | } |
||
| 442 | |||
| 443 | foreach ( $fieldset as $key => $field ) { |
||
| 444 | |||
| 445 | if ( ! isset( $field['type'] ) ) { |
||
| 446 | $field['type'] = 'text'; |
||
| 447 | } |
||
| 448 | |||
| 449 | // Get Value |
||
| 450 | switch ( $field['type'] ) { |
||
| 451 | case "checkbox" : |
||
| 452 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? 1 : 0; |
||
| 453 | break; |
||
| 454 | case "multiselect" : |
||
| 455 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? implode( ', ', array_map( 'wc_clean', $_POST[ $key ] ) ) : ''; |
||
| 456 | break; |
||
| 457 | case "textarea" : |
||
| 458 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? wp_strip_all_tags( wp_check_invalid_utf8( stripslashes( $_POST[ $key ] ) ) ) : ''; |
||
| 459 | break; |
||
| 460 | default : |
||
| 461 | $this->posted[ $key ] = isset( $_POST[ $key ] ) ? ( is_array( $_POST[ $key ] ) ? array_map( 'wc_clean', $_POST[ $key ] ) : wc_clean( $_POST[ $key ] ) ) : ''; |
||
| 462 | break; |
||
| 463 | } |
||
| 464 | |||
| 465 | // Hooks to allow modification of value |
||
| 466 | $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_' . sanitize_title( $field['type'] ) . '_field', $this->posted[ $key ] ); |
||
| 467 | $this->posted[ $key ] = apply_filters( 'woocommerce_process_checkout_field_' . $key, $this->posted[ $key ] ); |
||
| 468 | |||
| 469 | // Validation: Required fields |
||
| 470 | if ( isset( $field['required'] ) && $field['required'] && ( ! isset( $this->posted[ $key ] ) || "" === $this->posted[ $key ] ) ) { |
||
| 471 | switch ( $fieldset_key ) { |
||
| 472 | case 'shipping' : |
||
| 473 | $field_label = sprintf( _x( 'Shipping %s', 'Shipping FIELDNAME', 'woocommerce' ), $field['label'] ); |
||
| 474 | break; |
||
| 475 | case 'billing' : |
||
| 476 | $field_label = sprintf( _x( 'Billing %s', 'Billing FIELDNAME', 'woocommerce' ), $field['label'] ); |
||
| 477 | break; |
||
| 478 | default : |
||
| 479 | $field_label = $field['label']; |
||
| 480 | break; |
||
| 481 | } |
||
| 482 | wc_add_notice( apply_filters( 'woocommerce_checkout_required_field_notice', sprintf( _x( '%s is a required field.', 'FIELDNAME is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' ), $field_label ), 'error' ); |
||
| 483 | } |
||
| 484 | |||
| 485 | if ( ! empty( $this->posted[ $key ] ) ) { |
||
| 486 | |||
| 487 | // Validation rules |
||
| 488 | if ( ! empty( $field['validate'] ) && is_array( $field['validate'] ) ) { |
||
| 489 | foreach ( $field['validate'] as $rule ) { |
||
| 490 | switch ( $rule ) { |
||
| 491 | case 'postcode' : |
||
| 492 | $this->posted[ $key ] = strtoupper( str_replace( ' ', '', $this->posted[ $key ] ) ); |
||
| 493 | |||
| 494 | if ( ! WC_Validation::is_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ) ) : |
||
| 495 | wc_add_notice( __( 'Please enter a valid postcode/ZIP.', 'woocommerce' ), 'error' ); |
||
| 496 | else : |
||
| 497 | $this->posted[ $key ] = wc_format_postcode( $this->posted[ $key ], $_POST[ $fieldset_key . '_country' ] ); |
||
| 498 | endif; |
||
| 499 | break; |
||
| 500 | View Code Duplication | case 'phone' : |
|
| 501 | $this->posted[ $key ] = wc_format_phone_number( $this->posted[ $key ] ); |
||
| 502 | |||
| 503 | if ( ! WC_Validation::is_phone( $this->posted[ $key ] ) ) |
||
| 504 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid phone number.', 'woocommerce' ), 'error' ); |
||
| 505 | break; |
||
| 506 | View Code Duplication | case 'email' : |
|
| 507 | $this->posted[ $key ] = strtolower( $this->posted[ $key ] ); |
||
| 508 | |||
| 509 | if ( ! is_email( $this->posted[ $key ] ) ) |
||
| 510 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not a valid email address.', 'woocommerce' ), 'error' ); |
||
| 511 | break; |
||
| 512 | case 'state' : |
||
| 513 | // Get valid states |
||
| 514 | $valid_states = WC()->countries->get_states( isset( $_POST[ $fieldset_key . '_country' ] ) ? $_POST[ $fieldset_key . '_country' ] : ( 'billing' === $fieldset_key ? WC()->customer->get_billing_country() : WC()->customer->get_shipping_country() ) ); |
||
| 515 | |||
| 516 | if ( ! empty( $valid_states ) && is_array( $valid_states ) ) { |
||
| 517 | $valid_state_values = array_flip( array_map( 'strtolower', $valid_states ) ); |
||
| 518 | |||
| 519 | // Convert value to key if set |
||
| 520 | if ( isset( $valid_state_values[ strtolower( $this->posted[ $key ] ) ] ) ) { |
||
| 521 | $this->posted[ $key ] = $valid_state_values[ strtolower( $this->posted[ $key ] ) ]; |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | // Only validate if the country has specific state options |
||
| 526 | if ( ! empty( $valid_states ) && is_array( $valid_states ) && sizeof( $valid_states ) > 0 ) { |
||
| 527 | if ( ! in_array( $this->posted[ $key ], array_keys( $valid_states ) ) ) { |
||
| 528 | wc_add_notice( '<strong>' . $field['label'] . '</strong> ' . __( 'is not valid. Please enter one of the following:', 'woocommerce' ) . ' ' . implode( ', ', $valid_states ), 'error' ); |
||
| 529 | } |
||
| 530 | } |
||
| 531 | break; |
||
| 532 | } |
||
| 533 | } |
||
| 534 | } |
||
| 535 | } |
||
| 536 | } |
||
| 537 | } |
||
| 538 | |||
| 539 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 540 | if ( isset( $this->posted['billing_country'] ) ) { |
||
| 541 | WC()->customer->set_billing_country( $this->posted['billing_country'] ); |
||
| 542 | } |
||
| 543 | if ( isset( $this->posted['billing_state'] ) ) { |
||
| 544 | WC()->customer->set_billing_state( $this->posted['billing_state'] ); |
||
| 545 | } |
||
| 546 | if ( isset( $this->posted['billing_postcode'] ) ) { |
||
| 547 | WC()->customer->set_billing_postcode( $this->posted['billing_postcode'] ); |
||
| 548 | } |
||
| 549 | |||
| 550 | // Shipping Information |
||
| 551 | if ( ! $skipped_shipping ) { |
||
| 552 | |||
| 553 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 554 | if ( isset( $this->posted['shipping_country'] ) ) { |
||
| 555 | WC()->customer->set_shipping_country( $this->posted['shipping_country'] ); |
||
| 556 | } |
||
| 557 | if ( isset( $this->posted['shipping_state'] ) ) { |
||
| 558 | WC()->customer->set_shipping_state( $this->posted['shipping_state'] ); |
||
| 559 | } |
||
| 560 | if ( isset( $this->posted['shipping_postcode'] ) ) { |
||
| 561 | WC()->customer->set_shipping_postcode( $this->posted['shipping_postcode'] ); |
||
| 562 | } |
||
| 563 | } else { |
||
| 564 | |||
| 565 | // Update customer location to posted location so we can correctly check available shipping methods |
||
| 566 | if ( isset( $this->posted['billing_country'] ) ) { |
||
| 567 | WC()->customer->set_shipping_country( $this->posted['billing_country'] ); |
||
| 568 | } |
||
| 569 | if ( isset( $this->posted['billing_state'] ) ) { |
||
| 570 | WC()->customer->set_shipping_state( $this->posted['billing_state'] ); |
||
| 571 | } |
||
| 572 | if ( isset( $this->posted['billing_postcode'] ) ) { |
||
| 573 | WC()->customer->set_shipping_postcode( $this->posted['billing_postcode'] ); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | WC()->customer->save(); |
||
| 578 | |||
| 579 | // Update cart totals now we have customer address |
||
| 580 | WC()->cart->calculate_totals(); |
||
| 581 | |||
| 582 | // Terms |
||
| 583 | if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && empty( $this->posted['terms'] ) && wc_get_page_id( 'terms' ) > 0 && apply_filters( 'woocommerce_checkout_show_terms', true ) ) { |
||
| 584 | wc_add_notice( __( 'You must accept our Terms & Conditions.', 'woocommerce' ), 'error' ); |
||
| 585 | } |
||
| 586 | |||
| 587 | if ( WC()->cart->needs_shipping() ) { |
||
| 588 | $shipping_country = WC()->customer->get_shipping_country(); |
||
| 589 | |||
| 590 | if ( empty( $shipping_country ) ) { |
||
| 591 | wc_add_notice( __( 'Please enter an address to continue.', 'woocommerce' ), 'error' ); |
||
| 592 | } elseif ( ! in_array( WC()->customer->get_shipping_country(), array_keys( WC()->countries->get_shipping_countries() ) ) ) { |
||
| 593 | wc_add_notice( sprintf( __( 'Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce' ), WC()->countries->shipping_to_prefix() . ' ' . WC()->customer->get_shipping_country() ), 'error' ); |
||
| 594 | } |
||
| 595 | |||
| 596 | // Validate Shipping Methods |
||
| 597 | $packages = WC()->shipping->get_packages(); |
||
| 598 | $this->shipping_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); |
||
| 599 | |||
| 600 | foreach ( $packages as $i => $package ) { |
||
| 601 | if ( ! isset( $package['rates'][ $this->shipping_methods[ $i ] ] ) ) { |
||
| 602 | wc_add_notice( __( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce' ), 'error' ); |
||
| 603 | $this->shipping_methods[ $i ] = ''; |
||
| 604 | } |
||
| 605 | } |
||
| 606 | } |
||
| 607 | |||
| 608 | if ( WC()->cart->needs_payment() ) { |
||
| 609 | // Payment Method |
||
| 610 | $available_gateways = WC()->payment_gateways->get_available_payment_gateways(); |
||
| 611 | |||
| 612 | if ( ! isset( $available_gateways[ $this->posted['payment_method'] ] ) ) { |
||
| 613 | $this->payment_method = ''; |
||
| 614 | wc_add_notice( __( 'Invalid payment method.', 'woocommerce' ), 'error' ); |
||
| 615 | } else { |
||
| 616 | $this->payment_method = $available_gateways[ $this->posted['payment_method'] ]; |
||
| 617 | $this->payment_method->validate_fields(); |
||
| 618 | } |
||
| 619 | } else { |
||
| 620 | $available_gateways = array(); |
||
| 621 | } |
||
| 622 | |||
| 623 | // Action after validation |
||
| 624 | do_action( 'woocommerce_after_checkout_validation', $this->posted ); |
||
| 625 | |||
| 626 | if ( ! isset( $_POST['woocommerce_checkout_update_totals'] ) && wc_notice_count( 'error' ) == 0 ) { |
||
| 627 | |||
| 628 | // Customer accounts |
||
| 629 | $this->customer_id = apply_filters( 'woocommerce_checkout_customer_id', get_current_user_id() ); |
||
| 630 | |||
| 631 | if ( ! is_user_logged_in() && ( $this->must_create_account || ! empty( $this->posted['createaccount'] ) ) ) { |
||
| 632 | |||
| 633 | $username = ! empty( $this->posted['account_username'] ) ? $this->posted['account_username'] : ''; |
||
| 634 | $password = ! empty( $this->posted['account_password'] ) ? $this->posted['account_password'] : ''; |
||
| 635 | $new_customer = wc_create_new_customer( $this->posted['billing_email'], $username, $password ); |
||
| 636 | |||
| 637 | if ( is_wp_error( $new_customer ) ) { |
||
| 638 | throw new Exception( $new_customer->get_error_message() ); |
||
| 639 | } else { |
||
| 640 | $this->customer_id = absint( $new_customer ); |
||
| 641 | } |
||
| 642 | |||
| 643 | wc_set_customer_auth_cookie( $this->customer_id ); |
||
| 644 | |||
| 645 | // As we are now logged in, checkout will need to refresh to show logged in data |
||
| 646 | WC()->session->set( 'reload_checkout', true ); |
||
| 647 | |||
| 648 | // Also, recalculate cart totals to reveal any role-based discounts that were unavailable before registering |
||
| 649 | WC()->cart->calculate_totals(); |
||
| 650 | |||
| 651 | // Add customer info from other billing fields |
||
| 652 | if ( $this->posted['billing_first_name'] && apply_filters( 'woocommerce_checkout_update_customer_data', true, $this ) ) { |
||
| 653 | $userdata = array( |
||
| 654 | 'ID' => $this->customer_id, |
||
| 655 | 'first_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', |
||
| 656 | 'last_name' => $this->posted['billing_last_name'] ? $this->posted['billing_last_name'] : '', |
||
| 657 | 'display_name' => $this->posted['billing_first_name'] ? $this->posted['billing_first_name'] : '', |
||
| 658 | ); |
||
| 659 | wp_update_user( apply_filters( 'woocommerce_checkout_customer_userdata', $userdata, $this ) ); |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | // Do a final stock check at this point |
||
| 664 | $this->check_cart_items(); |
||
| 665 | |||
| 666 | // Abort if errors are present |
||
| 667 | if ( wc_notice_count( 'error' ) > 0 ) { |
||
| 668 | return false; |
||
| 669 | } |
||
| 670 | |||
| 671 | $order_id = $this->create_order(); |
||
| 672 | |||
| 673 | if ( is_wp_error( $order_id ) ) { |
||
| 674 | throw new Exception( $order_id->get_error_message() ); |
||
| 675 | } |
||
| 676 | |||
| 677 | do_action( 'woocommerce_checkout_order_processed', $order_id, $this->posted ); |
||
| 678 | |||
| 679 | // Process payment |
||
| 680 | if ( WC()->cart->needs_payment() ) { |
||
| 681 | |||
| 682 | // Store Order ID in session so it can be re-used after payment failure |
||
| 683 | WC()->session->order_awaiting_payment = $order_id; |
||
| 684 | |||
| 685 | // Process Payment |
||
| 686 | $result = $available_gateways[ $this->posted['payment_method'] ]->process_payment( $order_id ); |
||
| 687 | |||
| 688 | // Redirect to success/confirmation/payment page |
||
| 689 | if ( isset( $result['result'] ) && 'success' === $result['result'] ) { |
||
| 690 | |||
| 691 | $result = apply_filters( 'woocommerce_payment_successful_result', $result, $order_id ); |
||
| 692 | |||
| 693 | if ( is_ajax() ) { |
||
| 694 | wp_send_json( $result ); |
||
| 695 | } else { |
||
| 696 | wp_redirect( $result['redirect'] ); |
||
| 697 | exit; |
||
| 698 | } |
||
| 699 | } |
||
| 700 | } else { |
||
| 701 | |||
| 702 | if ( empty( $order ) ) { |
||
| 703 | $order = wc_get_order( $order_id ); |
||
| 704 | } |
||
| 705 | |||
| 706 | // No payment was required for order |
||
| 707 | $order->payment_complete(); |
||
| 708 | |||
| 709 | // Empty the Cart |
||
| 710 | WC()->cart->empty_cart(); |
||
| 711 | |||
| 712 | // Get redirect |
||
| 713 | $return_url = $order->get_checkout_order_received_url(); |
||
| 714 | |||
| 715 | // Redirect to success/confirmation/payment page |
||
| 716 | if ( is_ajax() ) { |
||
| 717 | wp_send_json( array( |
||
| 718 | 'result' => 'success', |
||
| 719 | 'redirect' => apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order ), |
||
| 720 | ) ); |
||
| 721 | } else { |
||
| 722 | wp_safe_redirect( |
||
| 723 | apply_filters( 'woocommerce_checkout_no_payment_needed_redirect', $return_url, $order ) |
||
| 724 | ); |
||
| 725 | exit; |
||
| 726 | } |
||
| 727 | } |
||
| 728 | } |
||
| 729 | } catch ( Exception $e ) { |
||
| 730 | if ( ! empty( $e ) ) { |
||
| 731 | wc_add_notice( $e->getMessage(), 'error' ); |
||
| 732 | } |
||
| 733 | } |
||
| 734 | |||
| 735 | // If we reached this point then there were errors |
||
| 736 | if ( is_ajax() ) { |
||
| 737 | |||
| 738 | // only print notices if not reloading the checkout, otherwise they're lost in the page reload |
||
| 739 | if ( ! isset( WC()->session->reload_checkout ) ) { |
||
| 740 | ob_start(); |
||
| 741 | wc_print_notices(); |
||
| 742 | $messages = ob_get_clean(); |
||
| 743 | } |
||
| 744 | |||
| 745 | $response = array( |
||
| 746 | 'result' => 'failure', |
||
| 747 | 'messages' => isset( $messages ) ? $messages : '', |
||
| 748 | 'refresh' => isset( WC()->session->refresh_totals ) ? 'true' : 'false', |
||
| 749 | 'reload' => isset( WC()->session->reload_checkout ) ? 'true' : 'false', |
||
| 750 | ); |
||
| 751 | |||
| 752 | unset( WC()->session->refresh_totals, WC()->session->reload_checkout ); |
||
| 753 | |||
| 754 | wp_send_json( $response ); |
||
| 755 | } |
||
| 756 | } |
||
| 757 | |||
| 836 |
This check marks private properties in classes that are never used. Those properties can be removed.