Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::process_checkbox_fieldprivateWC 1.0

Processes the options for a checkbox field and returns the new field_options array.

Method of the class: CheckoutFields{}

No Hooks.

Returns

Array|false. The updated $field_data array or false if an error was encountered.

Usage

// private - for code of main (parent) class only
$result = $this->process_checkbox_field( $field_data, $options );
$field_data(array) (required)
The field data array to be updated.
$options(array) (required)
The options supplied during field registration.

CheckoutFields::process_checkbox_field() code WC 10.9.4

private function process_checkbox_field( $field_data, $options ) {
	$id                     = $options['id'];
	$field_data['required'] = $options['required'] ?? false;

	if ( false === $field_data['required'] && ! empty( $options['error_message'] ) ) {
		$message = sprintf( 'Passing an error message to a non-required checkbox "%s" will have no effect. The error message has been removed from the field.', $id );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '9.8.0' );
		unset( $field_data['error_message'] );
	}

	if ( isset( $options['error_message'] ) && ! is_string( $options['error_message'] ) ) {
		$message = sprintf( 'The error_message property for field with id: "%s" must be a string, you passed %s. A default message will be shown.', $id, gettype( $options['error_message'] ) );
		_doing_it_wrong( 'woocommerce_register_additional_checkout_field', esc_html( $message ), '9.8.0' );
		unset( $field_data['error_message'] );
	}

	// Get the error message property and set it to errorMessage for use in JS.
	if ( isset( $field_data['error_message'] ) ) {
		$field_data['errorMessage'] = $field_data['error_message'];
		unset( $field_data['error_message'] );
	}

	return $field_data;
}