Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::get_field_from_object()publicWC 1.0

Returns a field value for a given object.

Method of the class: CheckoutFields{}

Return

Mixed. The field value.

Usage

$CheckoutFields = new CheckoutFields();
$CheckoutFields->get_field_from_object( $key, $wc_object, $group );
$key(string) (required)
The field key.
$wc_object(WC_Customer|WC_Order) (required)
The customer or order to get the field value for.
$group(string)
The group to get the field value for (shipping|billing|other).
Default: 'other'

CheckoutFields::get_field_from_object() code WC 9.1.2

public function get_field_from_object( string $key, WC_Data $wc_object, string $group = 'other' ) {
	if ( 'additional' === $group ) {
		wc_deprecated_argument( 'group', '8.9.0', 'The "additional" group is deprecated. Use "other" instead.' );
		$group = 'other';
	}

	$meta_key = self::get_group_key( $group ) . $key;

	$value = $wc_object->get_meta( $meta_key, true );

	if ( ! $value ) {
		/**
		 * 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_{$key}", null, $group, $wc_object );
	}

	// We cast the value to a boolean if the field is a checkbox.
	if ( $this->is_field( $key ) && 'checkbox' === $this->additional_fields[ $key ]['type'] ) {
		return '1' === $value;
	}

	if ( null === $value ) {
		return '';
	}

	return $value;
}