> ## Documentation Index
> Fetch the complete documentation index at: https://docs.smartwpplugins.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Default Swaps & Duplicate Detection

> Why reassigning a default never loses data, and how Unwan recognizes the same address twice

Two rules make the single-library model safe to use without customers ever "losing" an address by accident: default swaps are lossless, and duplicates are recognized automatically.

## Lossless default swaps

When an entry is promoted to a default role it didn't already hold, Unwan doesn't just overwrite the old default and discard it — it checks whether that displaced address is still represented anywhere else in the book first.

**1. The new address is written to the profile field**

Whatever entry is being promoted has its fields written into the WooCommerce billing or shipping profile field it's now claiming.

**2. The displaced address is checked**

Unwan reads what *used* to be in that profile field, before the overwrite.

**3. It's dropped, or preserved**

If the displaced address is empty, or its normalized signature matches the address that just replaced it, nothing further happens. Otherwise, it's inserted into the shared additional-address collection — so it's still in the book, just without a default role anymore.

> [!WARNING]
>
> A displaced default **bypasses the configured additional-address limit**. Changing which address holds a role must never be the reason an address disappears from the book — so this specific insertion skips the `unwan_address_save_limit` check that normally applies when a customer manually adds a new address.

### Worked example

A customer's billing default is *123 Main St*. They add a new address, *456 Oak Ave*, and check "Default billing address" while saving it.

1. *456 Oak Ave* is written into the billing profile field.
2. Unwan notices *123 Main St* used to be there and doesn't match *456 Oak Ave*.
3. *123 Main St* is added to the shared collection as a plain, role-free additional address — even if the customer was already at their saved-address limit.

The customer now has both addresses in their book; nothing was deleted.

## Duplicate detection

Before any address is saved — whether typed into My Account, entered at checkout, or displaced by a default swap — Unwan checks it against every existing entry (both profile defaults and every additional address) using a normalized signature:

```text theme={null}
signature = normalize(first_name) + "|" + normalize(last_name) + "|" + normalize(address_1) + "|" + normalize(postcode) without spaces
```

where `normalize()` strips accents, collapses whitespace, and lowercases the value. Spaces in the postcode are removed entirely, so `SW1A 2AA` and `sw1a2aa` match. Company, apartment line, city, state, country, and phone are **not** part of the signature — the comparison is deliberately narrow, matched to how customers actually think of "the same address." An address with no street line at all never produces a signature and is treated as always-unique (it can't collide, but it also can't be detected as a duplicate).

> [!NOTE]
>
> The postcode joined the signature in 1.0.8. Before, an address that corrected only the postcode counted as a duplicate: a checkout save reused the old entry and dropped the correction, and the address book showed only one of the two. Now they're separate entries, including two defaults that differ only in postcode. After updating, a customer may see a second entry where an older one had been hidden this way; nothing is deleted, and the customer can delete the outdated one in My Account.

> [tab] New address at checkout
>
> If a customer types an address at checkout that matches an existing entry's signature, Unwan reuses the existing entry's ID instead of creating a new one — so filling in the same details twice never creates two records.

> [tab] Editing in My Account
>
> Saving an edit that would make an entry's signature match a *different* existing entry is rejected with an error ("That address is already in your address book"), rather than silently creating a collision.

> [tab] Billing = shipping at checkout
>
> If a customer enters identical billing and shipping addresses in the same checkout, the second save recognizes the first as a duplicate and reuses it — Unwan never creates two records for what is, in normalized terms, one address.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.