Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::register_checkout_fieldpublicWC 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 10.4.3

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'                      => '',
			/* translators: %s Field label. */
			'optionalLabel'              => sprintf( __( '%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' ),
			'validation'                 => [],
		],
	);

	$field_data['attributes'] = $this->register_field_attributes( $field_data['id'], $field_data['attributes'] );
	$field_data               = $this->process_field_options( $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'];
}