> ## 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.

<Steps>
  <Step title="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.
  </Step>

  <Step title="The displaced address is checked">
    Unwan reads what *used* to be in that profile field, before the overwrite.
  </Step>

  <Step title="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.
  </Step>
</Steps>

<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.
</Warning>

### 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)
```

where `normalize()` strips accents, collapses whitespace, and lowercases the value. Company, city, state, postcode, 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).

<Tabs>
  <Tab title="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>

  <Tab title="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>

  <Tab title="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.
  </Tab>
</Tabs>


## Related topics

- [FAQ & Troubleshooting](/unwan/faq.md)
- [Checkout Overview](/unwan/checkout/checkout-overview.md)
- [The Address Book Model](/unwan/concepts/address-book-model.md)
- [Architecture](/unwan/developers/architecture.md)
- [Changelog](/unwan/changelog.md)
