Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x]: AdministrativeArea not saved for United Kingdom addresses in Craft CMS 5 #16832

Closed
romainpoirier opened this issue Mar 5, 2025 · 1 comment
Assignees
Labels

Comments

@romainpoirier
Copy link

romainpoirier commented Mar 5, 2025

What happened?

Description

With craftcms/cms version 5.5.10 and craftcms/commerce version 5.2.11, addresses can be encoded. Depending on the selected country, an additional field administrativeArea appears when required. When displayed, this field is usually mandatory.

I retrieve the list of countries using:

{{ craft.app.addresses.getCountryRepository().getList() }}

And the list of administrativeAreas as follows:

{% set administrativeAreas = {} %}
{% for countryCode, countryName in allCountries %}
  {% set subdivisions = craft.app.addresses.getSubdivisionRepository().getList([countryCode]) %}
  {% set administrativeAreas = administrativeAreas | merge({ (countryCode): subdivisions }) %}
{% endfor %}

In the generated list, the United Kingdom has a full list of administrativeAreas, meaning this field should be displayed when selecting the country.

However, when saving an address from the front end, the administrativeArea is not stored in the back end.

The reason appears to be that in the back end, when selecting the United Kingdom, the administrativeArea field does not appear, whereas it does for other countries such as UA, US, etc.

Steps to reproduce

  1. Retrieve the list of administrative areas for all countries using craft.app.addresses.getSubdivisionRepository().getList([countryCode]).
  2. Select the United Kingdom as the country when submitting an address from the front end.
  3. Ensure that the administrativeArea field is visible and populated.
  4. Save the address and check the stored data in the back end.

Expected behavior

  • The administrativeArea field should be displayed for the United Kingdom in the back end when selected.
  • The submitted administrativeArea should be correctly stored in the database.

Actual behavior

  • The administrativeArea field is not displayed in the back end for the United Kingdom.
  • The submitted administrativeArea from the front end is not saved.

Additional Information

  • Craft CMS version: 5.5.10
  • Craft Commerce version: 5.2.11
  • PHP version: 8.2.27
  • Database type & version: MySQL 8.0.40

Craft CMS version

5.5.10

PHP version

8.2.27

Database type and version

MySQL 8.0.40

@lukeholder
Copy link
Member

lukeholder commented Mar 6, 2025

Hi @romainpoirier

This issue has been discussed previously here: craftcms/commerce#3363

The addressing package (commerceguys/addressing) that we are are using doesn't consider the administrativeArea used by GB so by default is not showing it.

Data for the library (commerceguys/addressing) used to come from Google (issue explaining why counties are not provided: google/libaddressinput#193). The commerceguys/addressing team has taken ownership of that: https://github.com/commerceguys/addressing/blob/master/README.md?plain=1#L19, so the data is there, but it not marked as a "used field" for "GB" in their data still.

You can make it available to the CP form and to be saved to addresses by making it a "used field" for that country like this:

\yii\base\Event::on(
    \craft\services\Addresses::class,
    \craft\services\Addresses::EVENT_DEFINE_USED_SUBDIVISION_FIELDS,
    function(\craft\events\DefineAddressFieldsEvent $event) {
        if ($event->countryCode === 'GB') {
            $event->fields[] = \CommerceGuys\Addressing\AddressFormat\AddressField::ADMINISTRATIVE_AREA;
        }
    }
);

Doing the above will make it show up as 'Province':

Image

If you need to rename the field label in the CP, you would use the EVENT_DEFINE_FIELD_LABEL event.

Related issues: #13361, #13361

I will discuss with the team whether it makes sense to make this the default.

Let me know if you have any questions. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants