0

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

5
  • Can you please add the error message in your question? Commented Sep 3, 2018 at 13:39
  • 1
    Try to get like echo $address->getStreet1(); and echo $address->getStreet2();. Commented Sep 3, 2018 at 13:47
  • @ mohit, tried that one, i didn't get it. Commented Sep 3, 2018 at 13:51
  • Have you tried print_r($address->getStreet()); Commented Sep 3, 2018 at 13:52
  • Yes, its not printing anything after using this code Commented Sep 3, 2018 at 14:17

2 Answers 2

3

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();
}
5
  • Yes it is coming in two lines not as array, it is coming as string Commented Sep 5, 2018 at 13:38
  • I just posted an edit with a shot at an "answer" that attempts to debug your values just as you commented, if you var_dump the $address->getStreet() instead of echoing does it report an array or a string? :) Commented Sep 5, 2018 at 13:48
  • @jafarpinjar did any of this help? Did you get any output from my last piece of debugging code?? Commented Sep 5, 2018 at 15:07
  • perfect tested in 2.4 Commented Nov 7, 2022 at 17:37
  • Thanks mate, glad it helped. Shame its never gonna get that accepted answer mark :D Commented Nov 8, 2022 at 19:10
0

You should check module-customer/Model/Address/AbstractAddress.php class.

and there you will find :

getStreet(), getStreetLine() and getStreetFull() functions. that is how you can get street line 1 and 2.

 /**
 * Retrieve street field of an address
 *
 * @return string[]
 */
public function getStreet()
{
    if (is_array($this->getStreetFull())) {
        return $this->getStreetFull();
    }
    return explode("\n", $this->getStreetFull());
}

/**
 * Get steet line by number
 *
 * @param int $number
 * @return string
 */
public function getStreetLine($number)
{
    $lines = $this->    getStreet();
    return isset($lines[$number - 1]) ? $lines[$number - 1] : '';
}

/**
 * Retrieve text of street lines, concatenated using LF symbol
 *
 * @return string
 */
public function getStreetFull()
{
    $street = $this->getData('street');
    return is_array($street) ? implode("\n", $street) : $street;
}
4
  • how this can be used in observer? Commented Sep 3, 2018 at 12:35
  • I am getting error, if i use this code also echo $address->getStreet(); Commented Sep 3, 2018 at 12:36
  • you should pass this class in conductor and then call its object, assuming you already have cutomer id on this page. Secondly, you should not be doing any data operation using observer instead use plugin. Commented Sep 3, 2018 at 12:48
  • It would be helpful, if you update me the code Commented Sep 3, 2018 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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