setPreferredLocale($this->resolveLocale($order)); $contact->setTags($this->resolveTags()); // The spec requires minLength 1 for these; omit empty values instead // of sending "" (omitted fields keep their stored value on PATCH). $firstname = $order->get_billing_first_name(); if ('' !== $firstname) { $contact->setFirstname($firstname); } $lastname = $order->get_billing_last_name(); if ('' !== $lastname) { $contact->setLastname($lastname); } $email = $order->get_billing_email(); if ('' !== $email) { $contact->setEmail($email); } $company = $order->get_billing_company(); if ('' !== $company) { $contact->setCompany($company); } $phone = $order->get_billing_phone(); if ('' !== $phone) { $contact->setPhone($phone); } $address1 = $order->get_billing_address_1(); if ('' !== $address1) { $contact->setAddress1($address1); } $address2 = $order->get_billing_address_2(); if ('' !== $address2) { $contact->setAddress2($address2); } $city = $order->get_billing_city(); if ('' !== $city) { $contact->setCity($city); } // Aivie validates state and country as choice lists of full names // ("Switzerland", "Zürich"), while WooCommerce stores ISO codes // ("CH", "ZH"). Map the codes; an unmappable value is omitted so the // whole contact is not rejected because of one optional field. $state = $order->get_billing_state(); $country = $order->get_billing_country(); if ('' !== $state) { $stateName = CountryStateMapper::stateName($country, $state); if (null !== $stateName) { $contact->setState($stateName); } else { Logger::warning('Omitting billing state: no matching Aivie choice.', [ 'order_id' => $order->get_id(), 'state' => $state, 'country' => $country, ]); } } $postcode = $order->get_billing_postcode(); if ('' !== $postcode) { $contact->setZipcode($postcode); } if ('' !== $country) { $countryName = CountryStateMapper::countryName($country); if (null !== $countryName) { $contact->setCountry($countryName); } else { Logger::warning('Omitting billing country: no matching Aivie choice.', [ 'order_id' => $order->get_id(), 'country' => $country, ]); } } return $contact; } private function resolveLocale(\WC_Order $order): string { $customerId = (int) $order->get_customer_id(); // 1. User locale for logged-in customers. if ($customerId > 0) { $locale = get_user_locale($customerId); if ('' !== $locale) { return $this->normalizeLocale($locale); } } // 2. WPML language meta. if (defined('ICL_LANGUAGE_CODE')) { $wpmlLang = $order->get_meta('wpml_language'); if ('' !== $wpmlLang) { return $this->normalizeLocale((string) $wpmlLang); } } // 3. Site default. return $this->normalizeLocale(get_locale()); } /** * Aivie expects a 2-5 character locale (de, de_CH). WordPress also uses * variants such as de_DE_formal, which are reduced to language_REGION. */ private function normalizeLocale(string $locale): string { $parts = explode('_', $locale); return implode('_', array_slice($parts, 0, 2)); } private function resolveTags(): array { $tags = Options::getTags(); if (! in_array('woocommerce', $tags, true)) { $tags[] = 'woocommerce'; } if (! in_array('customer', $tags, true)) { $tags[] = 'customer'; } return $tags; } }