Automattic\WooCommerce\Blocks\Domain\Services
CheckoutFields::validate_options
Validates the "base" options (id, label, location) and shows warnings if they're not supplied.
Method of the class: CheckoutFields{}
No Hooks.
Returns
bool. false if an error was encountered, true otherwise.
Usage
// private - for code of main (parent) class only $result = $this->validate_options( $options );
- $options(array) (required) (passed by reference — &)
- The options supplied during field registration.
CheckoutFields::validate_options() CheckoutFields::validate options code WC 11.2.2
private function validate_options( &$options ) {
if ( empty( $options['id'] ) ) {
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', 'A checkout field cannot be registered without an id.', '8.6.0' );
return false;
}
// Having fewer than 2 after exploding around a / means there is no namespace.
if ( count( explode( '/', $options['id'] ) ) < 2 ) {
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'A checkout field id must consist of namespace/name.' );
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
return false;
}
if ( empty( $options['location'] ) ) {
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field location is required.' );
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
return false;
}
if ( 'additional' === $options['location'] ) {
wc_deprecated_argument( 'location', '8.9.0', 'The "additional" location is deprecated. Use "order" instead.' );
$options['location'] = 'order';
}
if ( ! in_array( $options['location'], array_keys( $this->fields_locations ), true ) ) {
$message = sprintf( 'Unable to register field with id: "%s". %s', $options['id'], 'The field location is invalid.' );
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
return false;
}
// At this point, the essentials fields and its location should be set and valid.
$location = $options['location'];
$id = $options['id'];
// Check to see if field is already in the array.
if ( ! empty( $this->additional_fields[ $id ] ) || in_array( $id, $this->fields_locations[ $location ], true ) ) {
$message = sprintf( 'Unable to register field with id: "%s". %s', $id, 'The field is already registered.' );
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
return false;
}
if ( ! empty( $options['type'] ) && ! isset( $this->field_types[ $options['type'] ] ) ) {
$message = sprintf(
'Unable to register field with id: "%s". Registering a field with type "%s" is not supported. The supported types are: %s.',
$id,
$options['type'],
implode( ', ', array_keys( $this->field_types ) )
);
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '8.6.0' );
return false;
}
$field_type = $this->get_field_type( $options );
if ( isset( $options['validation'] ) && is_array( $options['validation'] ) ) {
$options['validation'] = $field_type->prepare_validation_schema( $options['validation'] );
}
return $field_type->validate_options( $options );
}