WC_Checkout::validate_checkout()protectedWC 3.0.0

Validates that the checkout has enough info to proceed.

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

Хуки из метода

Возвращает

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

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->validate_checkout( $data, $errors );
$data(массив) (обязательный) (передается по ссылке — &)
An array of posted data.
$errors(WP_Error) (обязательный) (передается по ссылке — &)
Validation errors.

Список изменений

С версии 3.0.0 Введена.

Код WC_Checkout::validate_checkout() WC 8.7.0

protected function validate_checkout( &$data, &$errors ) {
	$this->validate_posted_data( $data, $errors );
	$this->check_cart_items();

	// phpcs:ignore WordPress.Security.NonceVerification.Missing
	if ( empty( $data['woocommerce_checkout_update_totals'] ) && empty( $data['terms'] ) && ! empty( $data['terms-field'] ) ) {
		$errors->add( 'terms', __( 'Please read and accept the terms and conditions to proceed with your order.', 'woocommerce' ) );
	}

	if ( WC()->cart->needs_shipping() ) {
		$shipping_country = isset( $data['shipping_country'] ) ? $data['shipping_country'] : WC()->customer->get_shipping_country();

		if ( empty( $shipping_country ) ) {
			$errors->add( 'shipping', __( 'Please enter an address to continue.', 'woocommerce' ) );
		} elseif ( ! in_array( $shipping_country, array_keys( WC()->countries->get_shipping_countries() ), true ) ) {
			if ( WC()->countries->country_exists( $shipping_country ) ) {
				/* translators: %s: shipping location (prefix e.g. 'to' + ISO 3166-1 alpha-2 country code) */
				$errors->add( 'shipping', sprintf( __( 'Unfortunately <strong>we do not ship %s</strong>. Please enter an alternative shipping address.', 'woocommerce' ), WC()->countries->shipping_to_prefix() . ' ' . $shipping_country ) );
			}
		} else {
			$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );

			foreach ( WC()->shipping()->get_packages() as $i => $package ) {
				if ( ! isset( $chosen_shipping_methods[ $i ], $package['rates'][ $chosen_shipping_methods[ $i ] ] ) ) {
					$errors->add( 'shipping', __( 'No shipping method has been selected. Please double check your address, or contact us if you need any help.', 'woocommerce' ) );
				}
			}
		}
	}

	if ( WC()->cart->needs_payment() ) {
		$available_gateways = WC()->payment_gateways->get_available_payment_gateways();

		if ( ! isset( $available_gateways[ $data['payment_method'] ] ) ) {
			$errors->add( 'payment', __( 'Invalid payment method.', 'woocommerce' ) );
		} else {
			$available_gateways[ $data['payment_method'] ]->validate_fields();
		}
	}

	do_action( 'woocommerce_after_checkout_validation', $data, $errors );
}