WC_Checkout::validate_checkout() – Validates that the checkout has enough info to proceed.

You appear to be a bot. Output may be restricted

Description

Validates that the checkout has enough info to proceed.

Usage

WC_Checkout::validate_checkout( $data, $errors );

Parameters

$data
( array ) required – An array of posted data.
$errors
( WP_Error ) required – Validation errors.

Returns

void

Source

File name: woocommerce/includes/class-wc-checkout.php
Lines:

1 to 42 of 42
  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( $_POST['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 );
  }
 

 View on GitHub View on Trac