netcarver / FieldtypeStreetAddress

Inputfield and Fieldtype for postal address input/storage/output in ProcessWire. Based upon the address meta data from Google's libaddressinput project.
MIT License
4 stars 2 forks source link

Setting from api #1

Open chaddupuis opened 5 years ago

chaddupuis commented 5 years ago

Could you provide me with some guidance to set this data programmatically from the api. I tried something like: $p->P_Address->street_address = $d[address]; $p->P_Address->postal_code = $d[zip]; $p->P_Address->locality = $d[state]; $p->P_Address->country = 'US';

Also tried sending in a full address in a string (i.e. $p->P_Address = '123 street, town, st, 84848, us';)

No matter what I do it seems to leave the field empty even though all other fields are created as expected. Any pointers, or simple it's not possible, would be very welcome. Thanks!

-Chad.

netcarver commented 4 years ago

@yyhmsg Hello Chad,

There are a couple of things that might be happening here. Firstly, you need to set the country_iso field, not the country field, which then sets up the rest of the required fields for the address field. Next, according to Google's meta-data for US addresses, the A(ddress) C(ity/locality) S(tate/admin area) Z(postal_code) fields are required. So I'd expect something like this to work...

$p->of(false);
$p->P_Address->country_iso = 'US';
$p->P_Address->street_address = $d[address]; // The A field
$p->P_Address->locality = $d[city]; // The C field
$p->P_Address->admin_area = $d[state]; // The S field
$p->P_Address->postal_code = $d[zip]; // The Z field
$p->save('P_Address');

Does that work for you?

Also, if you need more lines for the street part of the address, you can use the street_address_2 and street_address_3 subfields.