Automattic\WooCommerce\StoreApi\Routes\V1

CheckoutOrder::update_billing_address()privateWC 1.0

Updates the current customer session using data from the request (e.g. address data).

Address session data is synced to the order itself later on by OrderController::update_order_from_cart()

Method of the class: CheckoutOrder{}

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->update_billing_address( $request );
$request(\WP_REST_Request) (required)
Full details about the request.

CheckoutOrder::update_billing_address() code WC 9.3.3

private function update_billing_address( \WP_REST_Request $request ) {
	$customer = wc()->customer;
	$billing  = $request['billing_address'];
	$shipping = $request['shipping_address'];

	// Billing address is a required field.
	foreach ( $billing as $key => $value ) {
		if ( is_callable( [ $customer, "set_billing_$key" ] ) ) {
			$customer->{"set_billing_$key"}( $value );
		}
	}

	// If shipping address (optional field) was not provided, set it to the given billing address (required field).
	$shipping_address_values = $shipping ?? $billing;

	foreach ( $shipping_address_values as $key => $value ) {
		if ( is_callable( [ $customer, "set_shipping_$key" ] ) ) {
			$customer->{"set_shipping_$key"}( $value );
		} elseif ( 'phone' === $key ) {
			$customer->update_meta_data( 'shipping_phone', $value );
		}
	}

	/**
	 * Fires when the Checkout Block/Store API updates a customer from the API request data.
	 *
	 * @since 8.2.0
	 *
	 * @param \WC_Customer $customer Customer object.
	 * @param \WP_REST_Request $request Full details about the request.
	 */
	do_action( 'woocommerce_store_api_checkout_update_customer_from_request', $customer, $request );

	$customer->save();

	$this->order->set_billing_address( $billing );
	$this->order->set_shipping_address( $shipping );
	$this->order->save();
	$this->order->calculate_totals();
}