Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::validate_required_additional_fields()publicWC 1.0

Validate required additional fields on request.

Method of the class: Checkout{}

No Hooks.

Return

null. Nothing (null).

Usage

$Checkout = new Checkout();
$Checkout->validate_required_additional_fields( $request );
$request(\WP_REST_Request) (required)
Request object.

Checkout::validate_required_additional_fields() code WC 9.3.3

public function validate_required_additional_fields( \WP_REST_Request $request ) {
	$contact_fields           = $this->additional_fields_controller->get_fields_for_location( 'contact' );
	$order_fields             = $this->additional_fields_controller->get_fields_for_location( 'order' );
	$order_and_contact_fields = array_merge( $contact_fields, $order_fields );

	if ( ! empty( $order_and_contact_fields ) ) {
		foreach ( $order_and_contact_fields as $field_key => $order_and_contact_field ) {
			if ( $order_and_contact_field['required'] && ! isset( $request['additional_fields'][ $field_key ] ) ) {
				throw new RouteException(
					'woocommerce_rest_checkout_missing_required_field',
					/* translators: %s: is the field label */
					esc_html( sprintf( __( 'There was a problem with the provided additional fields: %s is required', 'woocommerce' ), $order_and_contact_field['label'] ) ),
					400
				);
			}
		}
	}

	$address_fields = $this->additional_fields_controller->get_fields_for_location( 'address' );
	if ( ! empty( $address_fields ) ) {
		$needs_shipping = WC()->cart->needs_shipping();
		foreach ( $address_fields as $field_key => $address_field ) {
			if ( $address_field['required'] && ! isset( $request['billing_address'][ $field_key ] ) ) {
				throw new RouteException(
					'woocommerce_rest_checkout_missing_required_field',
					/* translators: %s: is the field label */
					esc_html( sprintf( __( 'There was a problem with the provided billing address: %s is required', 'woocommerce' ), $address_field['label'] ) ),
					400
				);
			}
			if ( $needs_shipping && $address_field['required'] && ! isset( $request['shipping_address'][ $field_key ] ) ) {
				throw new RouteException(
					'woocommerce_rest_checkout_missing_required_field',
					/* translators: %s: is the field label */
					esc_html( sprintf( __( 'There was a problem with the provided shipping address: %s is required', 'woocommerce' ), $address_field['label'] ) ),
					400
				);
			}
		}
	}
}