Any ideas how to add custom fields to the address Model?

I want to add a free text input for County, so that it can be used for the equivalent in any country, as the shop sells to more than UK.

Upon doing so, I get the following error when trying to edit an address;

Craft\Commerce_AddressModel and its behaviors do not have a method or closure named "county".
  • There's no UI for this in the control panel since addresses aren't an element type. Did you get that error in the context of a plugin where you tried to extend the existing Commerce_AddressModel?
    – Brad Bell
    Dec 9 '16 at 19:13

Currently (Dec '16) - custom fields are not able to be used with addresses in Commerce.

Your best best is probably to add a field to your orders and pretend it's on addresses, but obviously if you deal with multiple addresses, as you probably do, that gets a bit tricky quickly. You could implement a plugin that has it's own table of records that associate addressIDs with a county field.

I am pretty sure it's on the Commerce radar but you're a bit stuck right now I think, without some extra work.

  • I think I might have to make it work for UK county using the regions field and hide this away/disable for other EU countries. Dec 9 '16 at 23:50
  • You could also put all in for all countries (UK - Suffolk for example) and filter on the front end by country Dec 10 '16 at 1:50

Currently (Dec '16) I have managed to get around this by re-using the Region/State field and passing this information through as a text field input that the user can enter.

<label class="label" for="{{ modelName }}-stateName">County</label>
<input type="text" id="{{ modelName }}-stateName" name="{{ modelName }}[stateName]" value="{{ model.stateName}}">

This ensures that the County is saved in the address model. It can be access as per any other address field in the templates.

I set the back-end has all States/Regions to be empty. But the user-entered text field is still stored against each customer saved address or order billing/shipping addresses.

The only downfall at the moment, is that in the back-end this is stored out-of order in terms of a structured UK address. The system stores this AFTER the postcode/zip code field. So an address format looks like this; Address Line 1
Address Line 2

Rather than

Address Line 1
Address Line 2
Region Postcode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.