Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::get_all_fields_from_object()publicWC 1.0

Returns an array of all fields values for a given object in a group.

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

Возвращает

Массив. An array of fields.

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

$CheckoutFields = new CheckoutFields();
$CheckoutFields->get_all_fields_from_object( $wc_object, $group, $all );
$wc_object(WC_Data) (обязательный)
The object or order to get the fields for.
$group(строка)
The group to get the fields for (shipping|billing|other).
По умолчанию: 'other'
$all(true|false)
Whether to return all fields or only the ones that are still registered.
По умолчанию: false

Код CheckoutFields::get_all_fields_from_object() WC 9.8.1

public function get_all_fields_from_object( WC_Data $wc_object, string $group = 'other', bool $all = false ) {
	$meta_data = [];
	$group     = $this->prepare_group_name( $group );
	$prefix    = self::get_group_key( $group );

	if ( $wc_object instanceof WC_Data ) {
		$meta = $wc_object->get_meta_data();
		foreach ( $meta as $meta_data_object ) {
			if ( 0 === \strpos( $meta_data_object->key, $prefix ) ) {
				$key = \str_replace( $prefix, '', $meta_data_object->key );
				if ( $all || $this->is_field( $key ) ) {
					$meta_data[ $key ] = $meta_data_object->value;
				}
			}
		}
	}

	$missing_fields = array_diff( array_keys( $this->get_fields_for_group( $group ) ), array_keys( $meta_data ) );

	foreach ( $missing_fields as $missing_field ) {
			/**
			 * Allow providing a default value for additional fields if no value is already set.
			 *
			 * @param null $value The default value for the filter, always null.
			 * @param string $group The group of this key (shipping|billing|other).
			 * @param WC_Data $wc_object The object to get the field value for.
			 *
			 * @since 8.9.0
			 */
			$value = apply_filters( "woocommerce_get_default_value_for_{$missing_field}", null, $group, $wc_object );

		if ( isset( $value ) ) {
			$meta_data[ $missing_field ] = $value;
		}
	}

	return $meta_data;
}