> */ public static function resolve( int $user_id, string $user_email = '', $checkout = null ): array { if ( $user_id <= 0 || ! function_exists( 'WC' ) ) { return [ 'billing_address' => [], 'shipping_address' => [], ]; } $customer = new \WC_Customer( $user_id ); $user = get_userdata( $user_id ); $account_first_name = $user instanceof \WP_User ? trim( (string) $user->first_name ) : ''; $account_last_name = $user instanceof \WP_User ? trim( (string) $user->last_name ) : ''; $billing = [ 'first_name' => self::resolve_checkout_value( $checkout, 'billing_first_name', self::resolve_customer_value( $user_id, $customer, 'get_billing_first_name', 'billing_first_name', $account_first_name ) ), 'last_name' => self::resolve_checkout_value( $checkout, 'billing_last_name', self::resolve_customer_value( $user_id, $customer, 'get_billing_last_name', 'billing_last_name', $account_last_name ) ), 'company' => self::resolve_checkout_value( $checkout, 'billing_company', self::resolve_customer_value( $user_id, $customer, 'get_billing_company', 'billing_company' ) ), 'address_1' => self::resolve_checkout_value( $checkout, 'billing_address_1', self::resolve_customer_value( $user_id, $customer, 'get_billing_address_1', 'billing_address_1' ) ), 'address_2' => self::resolve_checkout_value( $checkout, 'billing_address_2', self::resolve_customer_value( $user_id, $customer, 'get_billing_address_2', 'billing_address_2' ) ), 'city' => self::resolve_checkout_value( $checkout, 'billing_city', self::resolve_customer_value( $user_id, $customer, 'get_billing_city', 'billing_city' ) ), 'state' => self::resolve_checkout_value( $checkout, 'billing_state', self::resolve_customer_value( $user_id, $customer, 'get_billing_state', 'billing_state' ) ), 'postcode' => self::resolve_checkout_value( $checkout, 'billing_postcode', self::resolve_customer_value( $user_id, $customer, 'get_billing_postcode', 'billing_postcode' ) ), 'country' => self::resolve_checkout_value( $checkout, 'billing_country', self::resolve_customer_value( $user_id, $customer, 'get_billing_country', 'billing_country' ) ), 'phone' => self::resolve_checkout_value( $checkout, 'billing_phone', self::resolve_customer_value( $user_id, $customer, 'get_billing_phone', 'billing_phone' ) ), 'email' => self::resolve_checkout_value( $checkout, 'billing_email', self::resolve_customer_value( $user_id, $customer, 'get_billing_email', 'billing_email', $user_email ), $user_email ), ]; $shipping = [ 'first_name' => self::resolve_checkout_value( $checkout, 'shipping_first_name', self::resolve_customer_value( $user_id, $customer, 'get_shipping_first_name', 'shipping_first_name', $billing['first_name'] ), $billing['first_name'] ), 'last_name' => self::resolve_checkout_value( $checkout, 'shipping_last_name', self::resolve_customer_value( $user_id, $customer, 'get_shipping_last_name', 'shipping_last_name', $billing['last_name'] ), $billing['last_name'] ), 'company' => self::resolve_checkout_value( $checkout, 'shipping_company', self::resolve_customer_value( $user_id, $customer, 'get_shipping_company', 'shipping_company', $billing['company'] ), $billing['company'] ), 'address_1' => self::resolve_checkout_value( $checkout, 'shipping_address_1', self::resolve_customer_value( $user_id, $customer, 'get_shipping_address_1', 'shipping_address_1', $billing['address_1'] ), $billing['address_1'] ), 'address_2' => self::resolve_checkout_value( $checkout, 'shipping_address_2', self::resolve_customer_value( $user_id, $customer, 'get_shipping_address_2', 'shipping_address_2', $billing['address_2'] ), $billing['address_2'] ), 'city' => self::resolve_checkout_value( $checkout, 'shipping_city', self::resolve_customer_value( $user_id, $customer, 'get_shipping_city', 'shipping_city', $billing['city'] ), $billing['city'] ), 'state' => self::resolve_checkout_value( $checkout, 'shipping_state', self::resolve_customer_value( $user_id, $customer, 'get_shipping_state', 'shipping_state', $billing['state'] ), $billing['state'] ), 'postcode' => self::resolve_checkout_value( $checkout, 'shipping_postcode', self::resolve_customer_value( $user_id, $customer, 'get_shipping_postcode', 'shipping_postcode', $billing['postcode'] ), $billing['postcode'] ), 'country' => self::resolve_checkout_value( $checkout, 'shipping_country', self::resolve_customer_value( $user_id, $customer, 'get_shipping_country', 'shipping_country', $billing['country'] ), $billing['country'] ), 'phone' => self::resolve_checkout_value( $checkout, 'shipping_phone', self::resolve_customer_value( $user_id, $customer, 'get_shipping_phone', 'shipping_phone', $billing['phone'] ), $billing['phone'] ), ]; return [ 'billing_address' => $billing, 'shipping_address' => $shipping, ]; } private static function resolve_customer_value( int $user_id, \WC_Customer $customer, string $getter, string $meta_key, string $secondary_fallback = '' ): string { $value = ''; if ( is_callable( [ $customer, $getter ] ) ) { $resolved = $customer->{$getter}(); if ( is_string( $resolved ) ) { $value = trim( $resolved ); } } if ( '' !== $value ) { return $value; } $meta_value = get_user_meta( $user_id, $meta_key, true ); if ( is_string( $meta_value ) ) { $meta_value = trim( $meta_value ); if ( '' !== $meta_value ) { return $meta_value; } } return trim( $secondary_fallback ); } private static function resolve_checkout_value( $checkout, string $field, string $fallback = '', string $secondary_fallback = '' ): string { $value = ''; if ( $checkout && is_callable( [ $checkout, 'get_value' ] ) ) { $resolved = $checkout->get_value( $field ); if ( is_string( $resolved ) ) { $value = trim( $resolved ); } } if ( '' !== $value ) { return $value; } $fallback = trim( $fallback ); if ( '' !== $fallback ) { return $fallback; } return trim( $secondary_fallback ); } }