Add county for US along with state.

Created on 3 August 2023, 11 months ago
Updated 6 May 2024, about 2 months ago

Unable to add county option for US

I followed the article mentioned in https://docs.drupalcommerce.org/commerce2/developer-guide/customers/addr... for US. But it is stopped taking states and getting the county which I added.

Can you help to add county along with states for US in address?

💬 Support request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India kuntal_d

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @kuntal_d
  • Status changed to Closed: won't fix 9 months ago
  • 🇷🇸Serbia bojanz

    I can't know what's wrong, and you've provided no information that I can act on. All I can suggest is re-reading the documentation and repeating the steps mentioned there.

  • Status changed to Active 5 months ago
  • I have the same need of adding counties as well as states. I have reviewed the link included in the description of this issue and based on that documentation it looks to me like they are describing how to modify the administrative_area subfield, not add a new subfield, which is what I need.

    How would I go about adding a county subfield?

  • 🇷🇸Serbia bojanz

    I misunderstood the request when I first closed this issue.

    Unfortunately, Address has no designated property for US counties (Google calls it `administrative_area_level2`), it's not a use case we support. Adding a new property from outside the module would be impossible, you'd have to essentially fork the entire module codebase. A workaround is needed: repurposing an existing property.

    We have two possible ways to achieve this:

    1) Repurpose address_line3 to hold the county.
    Implement a hook_field_widget_form_alter() + #after_build for the address element, which looks at the currently selected country, and if it's US, converts address_line3 to a dropdown, gives it a new label, and populates the options based on the administrative area.

    2) Repurpose the locality field to hold the county, and move the City to the dependent_locality field.
    This allows you to use SubdivisionEvent to define the counties per administrative area, and it will just work. An alter is only needed to adjust the labels. However, you need to teach all other code (payment gateways, shipping methods) to pass $address->getDependentLocality() instead of $address->getLocality() for the city.
    You also need an AddressFormatEvent to add the %dependentLocality to the format.

  • 🇺🇸United States dalemoore

    I was exploring this module and discovered it didn't support U.S. counties as well. This would be a great feature for using the module for local, regional, and state entities as well as some university units like where I work. A lot of our work is county-based at land-grant university Extension services. It would also be useful for sites related to voting, as people are restricted to only voting within their specific state, county, and city. County is missing in that scenario here.

Production build 0.69.0 2024