Magento – Address update get street problem magento 2

customer-addressevent-observermagento2

I am using "customer_address_save_after" event.

This is my observer code.

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $address = $observer->getEvent()->getCustomerAddress();    
    echo $address->getCity();     
    echo $address->getStreet(0);
    die;
}

I am getting city value, but street is throwing me error.

How to get street line 1 and line 2, The above code throwing me fatal error when i am trying to update shipping address from customer account page.

I am getting error after using below code also

 echo $address->getStreet();

Below is my form post data

    Array
 (
[entity_id] => 381939
[increment_id] => 
[parent_id] => 46195
[created_at] => 2018-08-07 08:44:12
[updated_at] => 2018-09-03 10:35:33
[is_active] => 1
[city] => Hampton
[company] => Test
[country_id] => GB
[fax] => 
[firstname] => Test
[lastname] => test
[middlename] => 
[postcode] => UG 1JY
[prefix] => 
[region] => Surrey
[region_id] => 0
[street] => London
 street
[suffix] => 
[telephone] => 9898989898
[vat_id] => 
[vat_is_valid] => 
[vat_request_date] => 
[vat_request_id] => 
[vat_request_success] => 
[region_code] => Surrey
[id] => 381939
[customer_id] => 46195
[default_shipping] => 
[default_billing] => 
[is_default_billing] => 
[is_default_shipping] => 
)

I am getting below error,

uncaught error: Array to string conversion street in file

Best Answer

Another angle on this, can you post your form code? (the <input /> elements specifically?

are you using the default templates? (ignore above if you are).

I often see issues like this if you post the values as a string instead of as an array as the code is expecting. For example I would expect the post to be an array, i.e. :

street => array( 
    [0] => 'line one',
    [1] => 'line 2'
) [...]

Not a string with a line break in it, this is normally what it is turned into on save, the street attribute would then turn it back into an array when it loads the value, as you see in Naveed's answer.

Is that actually your POST output (i.e. var_dump($_POST)) or an extract from the address object (i.e. $address->debug()).

Also, it would be useful to see what is going on with the address as it is passed from the observer. can you call $address->debug() in your observer please? I ask this as the observe you're working in has already parsed the POST array whil processing the customer save, created your address object and passed it to the observer. So you are looking at the $_POST too late, it's already been read and handled.

A shot at an answer :

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $address = $observer->getEvent()->getCustomerAddress();    
    echo $address->getCity();  //works   
    echo $address->getStreet(0); //fails, getStreet returns an array and echo tries to handle as a string

    //try this instead
    var_dump($address->getStreet()); //should return an array

    echo $address->getStreetLine(1); //first line
    echo $address->getStreetLine(2); //second line, might be an empty string

    //or
    echo $address->getStreet()[0]; //first line
    echo $address->getStreet()[1]; //second line, might fail if empty (array element does not exist)
    die();
}
Related Topic