Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::register_checkout_field()publicWC 1.0

Registers an additional field for Checkout.

Метод класса: CheckoutFields{}

Хуков нет.

Возвращает

WP_Error|null. True if the field was registered, a WP_Error otherwise.

Использование

$CheckoutFields = new CheckoutFields();
$CheckoutFields->register_checkout_field( $options );
$options(массив) (обязательный)
The field options.

Код CheckoutFields::register_checkout_field() WC 9.5.1

public function register_checkout_field( $options ) {
	// Check the options and show warnings if they're not supplied. Return early if an error that would prevent registration is encountered.
	if ( false === $this->validate_options( $options ) ) {
		return;
	}

	// The above validate_options function ensures these options are valid. Type might not be supplied but then it defaults to text.
	$field_data = wp_parse_args(
		$options,
		[
			'id'                         => '',
			'label'                      => '',
			'optionalLabel'              => sprintf(
				/* translators: %s Field label. */
				__( '%s (optional)', 'woocommerce' ),
				$options['label']
			),
			'location'                   => '',
			'type'                       => 'text',
			'hidden'                     => false,
			'required'                   => false,
			'attributes'                 => [],
			'show_in_order_confirmation' => true,
			'sanitize_callback'          => array( $this, 'default_sanitize_callback' ),
			'validate_callback'          => array( $this, 'default_validate_callback' ),
		]
	);

	$field_data['attributes'] = $this->register_field_attributes( $field_data['id'], $field_data['attributes'] );

	if ( 'checkbox' === $field_data['type'] ) {
		$field_data = $this->process_checkbox_field( $field_data, $options );
	} elseif ( 'select' === $field_data['type'] ) {
		$field_data = $this->process_select_field( $field_data, $options );
	}

	// $field_data will be false if an error that will prevent the field being registered is encountered.
	if ( false === $field_data ) {
		return;
	}

	// Insert new field into the correct location array.
	$this->additional_fields[ $field_data['id'] ]        = $field_data;
	$this->fields_locations[ $field_data['location'] ][] = $field_data['id'];
}