Automattic\WooCommerce\StoreApi\Schemas\V1

BillingAddressSchema::get_item_response()publicWC 1.0

Convert a term object into an object suitable for the response.

Method of the class: BillingAddressSchema{}

No Hooks.

Return

Array.

Usage

$BillingAddressSchema = new BillingAddressSchema();
$BillingAddressSchema->get_item_response( $address );
$address(\WC_Order|\WC_Customer) (required)
An object with billing address.

BillingAddressSchema::get_item_response() code WC 8.7.0

public function get_item_response( $address ) {
	$validation_util = new ValidationUtils();
	if ( ( $address instanceof \WC_Customer || $address instanceof \WC_Order ) ) {
		$billing_country = $address->get_billing_country();
		$billing_state   = $address->get_billing_state();

		if ( ! $validation_util->validate_state( $billing_state, $billing_country ) ) {
			$billing_state = '';
		}

		if ( $address instanceof \WC_Order ) {
			// get additional fields from order.
			$additional_address_fields = $this->additional_fields_controller->get_all_fields_from_order( $address );
		} elseif ( $address instanceof \WC_Customer ) {
			// get additional fields from customer.
			$additional_address_fields = $this->additional_fields_controller->get_all_fields_from_customer( $address );
		}

		$additional_address_fields = array_reduce(
			array_keys( $additional_address_fields ),
			function( $carry, $key ) use ( $additional_address_fields ) {
				if ( 0 === strpos( $key, '/billing/' ) ) {
					$value         = $additional_address_fields[ $key ];
					$key           = str_replace( '/billing/', '', $key );
					$carry[ $key ] = $value;
				}
				return $carry;
			},
			[]
		);
		$address_object            = \array_merge(
			[
				'first_name' => $address->get_billing_first_name(),
				'last_name'  => $address->get_billing_last_name(),
				'company'    => $address->get_billing_company(),
				'address_1'  => $address->get_billing_address_1(),
				'address_2'  => $address->get_billing_address_2(),
				'city'       => $address->get_billing_city(),
				'state'      => $billing_state,
				'postcode'   => $address->get_billing_postcode(),
				'country'    => $billing_country,
				'email'      => $address->get_billing_email(),
				'phone'      => $address->get_billing_phone(),
			],
			$additional_address_fields
		);

		// Add any missing keys from additional_fields_controller to the address response.
		foreach ( $this->additional_fields_controller->get_address_fields_keys() as $field ) {
			if ( isset( $address_object[ $field ] ) ) {
				continue;
			}
			$address_object[ $field ] = '';
		}

		foreach ( $address_object as $key => $value ) {
			if ( isset( $this->get_properties()[ $key ]['type'] ) && 'boolean' === $this->get_properties()[ $key ]['type'] ) {
				$address_object[ $key ] = (bool) $value;
			} else {
				$address_object[ $key ] = $this->prepare_html_response( $value );
			}
		}
		return $address_object;

	}
	throw new RouteException(
		'invalid_object_type',
		sprintf(
			/* translators: Placeholders are class and method names */
			__( '%1$s requires an instance of %2$s or %3$s for the address', 'woocommerce' ),
			'BillingAddressSchema::get_item_response',
			'WC_Customer',
			'WC_Order'
		),
		500
	);
}