WordPress at Your Fingertips
function is not described

WC_API_Orders::set_order_addresses() protected WC 2.1

Helper method to set/update the billing & shipping addresses for an order

{} It's a method of the class: WC_API_Orders{}

No Hooks.

Return

null. Nothing.

Usage

// protected - for code of main (parent) or child class
$result = $this->set_order_addresses( $order, $data );
$order(\WC_Order) (required)
-
$data(array) (required)
-

Changelog

Since 2.1 Introduced.

Code of WC_API_Orders::set_order_addresses() WC 5.6.0

protected function set_order_addresses( $order, $data ) {

	$address_fields = array(
		'first_name',
		'last_name',
		'company',
		'email',
		'phone',
		'address_1',
		'address_2',
		'city',
		'state',
		'postcode',
		'country',
	);

	$billing_address = $shipping_address = array();

	// billing address
	if ( isset( $data['billing_address'] ) && is_array( $data['billing_address'] ) ) {

		foreach ( $address_fields as $field ) {

			if ( isset( $data['billing_address'][ $field ] ) ) {
				$billing_address[ $field ] = wc_clean( $data['billing_address'][ $field ] );
			}
		}

		unset( $address_fields['email'] );
		unset( $address_fields['phone'] );
	}

	// shipping address
	if ( isset( $data['shipping_address'] ) && is_array( $data['shipping_address'] ) ) {

		foreach ( $address_fields as $field ) {

			if ( isset( $data['shipping_address'][ $field ] ) ) {
				$shipping_address[ $field ] = wc_clean( $data['shipping_address'][ $field ] );
			}
		}
	}

	$this->update_address( $order, $billing_address, 'billing' );
	$this->update_address( $order, $shipping_address, 'shipping' );

	// update user meta
	if ( $order->get_user_id() ) {
		foreach ( $billing_address as $key => $value ) {
			update_user_meta( $order->get_user_id(), 'billing_' . $key, $value );
		}
		foreach ( $shipping_address as $key => $value ) {
			update_user_meta( $order->get_user_id(), 'shipping_' . $key, $value );
		}
	}
}