How to access individual values within address attribute type

Permalink 1 user found helpful
Hi,

In a controller file, I need to access various parts of an "address" attribute (for shipping purposes using core_commerce). When I load the address value, the entire address is a single string of text.

I can do this myself using PHP to explode etc.. but it's very messy and might not always work if c5 decides to change the way it handles addresses. I'd like to do it properly if I can.

Is there anything built into c5 that I can use to split up this value and access the individual parts of an address, eg: postcode, address line 1 etc?
How would I do this?

 
mkly replied on at Permalink Best Answer Reply
mkly
Here's a snippet for getting this for a User

// Change to get the UserInfo object you want
$ui = UserInfo::getByUserName('admin');
// Change to the handle of your attribute
$address = $ui->getAttribute('shipping_address');
// A list of the available methods on the address attribute
if ($address) {
  echo $address->getAddress1();
  echo '<br/>';
  echo $address->getAddress2();
  echo '<br/>';
  echo $address->getCity();
  echo '<br/>';
  echo $address->getCountry();
  echo '<br/>';
  echo $address->getPostalCode();

Regards,
Mike
http://mkly.io
Becrea replied on at Permalink Reply
Becrea
mkly .. thanks
katiam replied on at Permalink Reply
Brilliant mkly- THANK YOU!!!!
bobbyschultz replied on at Permalink Reply
bobbyschultz
How about the state? I tried getState() getStateProvince() and getProvince() with no luck.

Or, where is the class defined, and I can look for myself.

thank you!
bobbyschultz replied on at Permalink Reply
bobbyschultz
Answered my own question:

$address->getStateProvince()