Automattic\WooCommerce\Blocks\Domain\Services
CheckoutFields::register_checkout_field
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() CheckoutFields::register checkout field WC 11.0.1
public function register_checkout_field( $options ) {
// Warn when fields are registered before `after_setup_theme`. Registering that early can cause problems, such as loading translations before they're ready.
if ( ! did_action( 'after_setup_theme' ) && ! doing_action( 'after_setup_theme' ) ) {
_doing_it_wrong( 'woocommerce_register_additional_checkout_field', 'Additional checkout fields should be registered on the woocommerce_init action or later.', '11.0.0' );
}
// 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'];
}