WC_Checkout::process_checkoutpublicWC 1.0

Process the checkout after the confirm order button is pressed.

Метод класса: WC_Checkout{}

Возвращает

null. Ничего (null).

Использование

$WC_Checkout = new WC_Checkout();
$WC_Checkout->process_checkout();

Код WC_Checkout::process_checkout() WC 10.4.3

public function process_checkout() {
	try {
		$nonce_value    = wc_get_var( $_REQUEST['woocommerce-process-checkout-nonce'], wc_get_var( $_REQUEST['_wpnonce'], '' ) ); // phpcs:ignore
		$expiry_message = sprintf(
			/* translators: %s: shop cart url */
			__( 'Sorry, your session has expired. <a href="%s" class="wc-backward">Return to shop</a>', 'woocommerce' ),
			esc_url( wc_get_page_permalink( 'shop' ) )
		);

		if ( empty( $nonce_value ) || ! wp_verify_nonce( $nonce_value, 'woocommerce-process_checkout' ) ) {
			// If the cart is empty, the nonce check failed because of session expiry.
			if ( WC()->cart->is_empty() ) {
				throw new Exception( $expiry_message );
			}

			WC()->session->set( 'refresh_totals', true );
			throw new Exception( __( 'We were unable to process your order, please try again.', 'woocommerce' ) );
		}

		wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
		wc_set_time_limit( 0 );

		do_action( 'woocommerce_before_checkout_process' );

		if ( WC()->cart->is_empty() ) {
			throw new Exception( $expiry_message );
		}

		wc_log_order_step( '[Shortcode #1] Place Order flow initiated', null, false, true );

		do_action( 'woocommerce_checkout_process' );

		$errors      = new WP_Error();
		$posted_data = $this->get_posted_data();

		try {
			// Update session for customer and totals.
			$this->update_session( $posted_data );
		} catch ( WC_Data_Exception $e ) {
			// Billing email will be validated later down in validate_posted_data. Skip it, throw other errors.
			if ( 'customer_invalid_billing_email' !== $e->getErrorCode() ) {
				throw new Exception( $e->getMessage(), $e->getCode(), $e->getPrevious() );
			}
		}

		wc_log_order_step( '[Shortcode #2] Session updated with checkout data and totals calculated' );

		// Validate posted data and cart items before proceeding.
		$this->validate_checkout( $posted_data, $errors );

		if ( empty( $errors->errors ) ) {
			wc_log_order_step( '[Shortcode #3] Checkout posted data validated', array( 'payment_method' => $posted_data['payment_method'] ) );
		}

		foreach ( $errors->errors as $code => $messages ) {
			$data = $errors->get_error_data( $code );
			foreach ( $messages as $message ) {
				wc_add_notice( $message, 'error', $data );
			}
		}

		if ( empty( $posted_data['woocommerce_checkout_update_totals'] ) && 0 === wc_notice_count( 'error' ) ) {
			$this->process_customer( $posted_data );
			$order_id = $this->create_order( $posted_data );
			$order    = wc_get_order( $order_id );

			if ( is_wp_error( $order_id ) ) {
				throw new Exception( $order_id->get_error_message() );
			}

			if ( ! $order ) {
				throw new Exception( __( 'Unable to create order.', 'woocommerce' ) );
			}

			wc_log_order_step(
				'[Shortcode #4] Validated/Created customer and created order object',
				array( 'order_object' => $order )
			);

			do_action( 'woocommerce_checkout_order_processed', $order_id, $posted_data, $order );

			wc_log_order_step(
				'[Shortcode #5] woocommerce_checkout_order_processed hook ran successfully',
				array( 'order_object' => $order )
			);

			/**
			 * Note that woocommerce_cart_needs_payment is only used in
			 * WC_Checkout::process_checkout() to keep backwards compatibility.
			 * Use woocommerce_order_needs_payment instead.
			 *
			 * Note that at this point you can't rely on the Cart Object anymore,
			 * since it could be empty see:
			 * https://github.com/woocommerce/woocommerce/issues/24631
			 */

			if ( apply_filters( 'woocommerce_cart_needs_payment', $order->needs_payment(), WC()->cart ) ) {
				$this->process_order_payment( $order_id, $posted_data['payment_method'] );
			} else {
				$this->process_order_without_payment( $order_id );
			}
		}
	} catch ( Exception $e ) {
		// Step logs the exception. If nothing abnormal occurred during the process_checkout flow, the log is removed.
		wc_log_order_step( '[Shortcode #EXPECTEDFAIL] ' . $e->getMessage(), array( 'error_code' => $e->getCode() ), true );
		wc_add_notice( $e->getMessage(), 'error' );
	}
	$this->send_ajax_failure_response();
}