Automattic\WooCommerce\StoreApi\Routes\V1

CartUpdateCustomer::validate_address_params()protectedWC 1.0

Validate address params now they are populated.

Method of the class: CartUpdateCustomer{}

No Hooks.

Return

\WP_Error|true.

Usage

// protected - for code of main (parent) or child class
$result = $this->validate_address_params( $request, $billing, $shipping );
$request(\WP_REST_Request) (required)
Request object.
$billing(array) (required)
Billing address.
$shipping(array) (required)
Shipping address.

CartUpdateCustomer::validate_address_params() code WC 9.3.3

protected function validate_address_params( $request, $billing, $shipping ) {
	$posted_billing  = isset( $request['billing_address'] );
	$posted_shipping = isset( $request['shipping_address'] );
	$invalid_params  = array();
	$invalid_details = array();

	if ( $posted_billing ) {
		$billing_validation_check = $this->schema->billing_address_schema->validate_callback( $billing, $request, 'billing_address' );

		if ( false === $billing_validation_check ) {
			$invalid_params['billing_address'] = __( 'Invalid parameter.', 'woocommerce' );
		} elseif ( is_wp_error( $billing_validation_check ) ) {
			$invalid_params['billing_address']  = implode( ' ', $billing_validation_check->get_error_messages() );
			$invalid_details['billing_address'] = \rest_convert_error_to_response( $billing_validation_check )->get_data();
		}
	}

	if ( $posted_shipping ) {
		$shipping_validation_check = $this->schema->shipping_address_schema->validate_callback( $shipping, $request, 'shipping_address' );

		if ( false === $shipping_validation_check ) {
			$invalid_params['shipping_address'] = __( 'Invalid parameter.', 'woocommerce' );
		} elseif ( is_wp_error( $shipping_validation_check ) ) {
			$invalid_params['shipping_address']  = implode( ' ', $shipping_validation_check->get_error_messages() );
			$invalid_details['shipping_address'] = \rest_convert_error_to_response( $shipping_validation_check )->get_data();
		}
	}

	if ( $invalid_params ) {
		return new \WP_Error(
			'rest_invalid_param',
			/* translators: %s: List of invalid parameters. */
			sprintf( __( 'Invalid parameter(s): %s', 'woocommerce' ), implode( ', ', array_keys( $invalid_params ) ) ),
			[
				'status'  => 400,
				'params'  => $invalid_params,
				'details' => $invalid_details,
			]
		);
	}

	return true;
}