Automattic\WooCommerce\Blocks\Domain\Services

CheckoutFields::get_order_additional_fields_with_valuespublicWC 1.0

Get additional fields for an order.

Method of the class: CheckoutFields{}

No Hooks.

Returns

Array. An array of fields definitions as well as their values formatted for display.

Usage

$CheckoutFields = new CheckoutFields();
$CheckoutFields->get_order_additional_fields_with_values( $order, $location, $group, $context );
$order(WC_Order) (required)
Order object.
$location(string) (required)
The location to get fields for (address|contact|order).
$group(string)
The group to get the field value for (shipping|billing|other).
Default: 'other'
$context(string)
The context to get the field value for (edit|view).
Default: 'edit'

CheckoutFields::get_order_additional_fields_with_values() code WC 9.8.5

public function get_order_additional_fields_with_values( WC_Order $order, string $location, string $group = 'other', string $context = 'edit' ) {

	// Because the Additional Checkout Fields API only applies to orders created with Store API, we should not
	// return any values unless it was created using Store API. This is mainly to prevent "empty" checkbox values
	// from being shown on the order confirmation page for orders placed using the shortcode. It's rare that this
	// will happen but not impossible.
	if ( 'store-api' !== $order->get_created_via() ) {
		return [];
	}

	$location           = $this->prepare_location_name( $location );
	$group              = $this->prepare_group_name( $group );
	$fields             = $this->get_fields_for_location( $location );
	$fields_with_values = [];

	foreach ( $fields as $field_key => $field ) {
		$value = $this->get_field_from_object( $field_key, $order, $group );

		if ( '' === $value || null === $value ) {
			continue;
		}

		if ( 'view' === $context ) {
			$value = $this->format_additional_field_value( $value, $field );
		}

		$field['value']                   = $value;
		$fields_with_values[ $field_key ] = $field;
	}

	return $fields_with_values;
}