Address update get street problem magento 2event for customer address frontend updateObserve customer address save, update collectionMagento add custom options to cartUpdate default address in customer_save_after observerCustomer address update code is always creating new addressGetting strange result querying carts when using multiple search filter groupsHow to return a JSON object with a custom REST API in Magento 2?Magento 2 Custom Validation on Address Book (street[1])Customer street address second line not getting populatedHow to get street address in customer edit form

Generate random number in Unity without class ambiguity

What is it exactly about flying a Flyboard across the English channel that made Zapata's thighs burn?

Is the first page of a novel really that important?

How do I know when and if a character requires a backstory?

Is it uncompelling to continue the story with lower stakes?

…down the primrose path

Does a humanoid possessed by a ghost register as undead to a paladin's Divine Sense?

Based on what criteria do you add/not add icons to labels within a toolbar?

Are valid inequalities worth the effort given modern solver preprocessing options?

What is the reason behind water not falling from a bucket at the top of loop?

I was contacted by a private bank overseas to get my inheritance

Why do proponents of guns oppose gun competency tests?

3 beeps on a 486 computer with an American Megatrends bios?

How can I perform a deterministic physics simulation?

How do the surviving Asgardians get to Earth?

Is there a booking app or site that lets you specify your gender for shared dormitories?

If someone else uploads my GPL'd code to Github without my permission, is that a copyright violation?

What does "autolyco-sentimental" mean?

What percentage of campground outlets are GFCI or RCD protected?

How can I use commands with sudo without changing owner of the files?

Is there a command-line tool for converting html files to pdf?

Did Logical Positivism fail because it simply denied human emotion?

GFCI tripping on overload?

What license to choose for my PhD thesis?



Address update get street problem magento 2


event for customer address frontend updateObserve customer address save, update collectionMagento add custom options to cartUpdate default address in customer_save_after observerCustomer address update code is always creating new addressGetting strange result querying carts when using multiple search filter groupsHow to return a JSON object with a custom REST API in Magento 2?Magento 2 Custom Validation on Address Book (street[1])Customer street address second line not getting populatedHow to get street address in customer edit form






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am using "customer_address_save_after" event.



This is my observer code.



public function execute(MagentoFrameworkEventObserver $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










share|improve this question


























  • Can you please add the error message in your question?

    – Mohit Kumar Arora
    Sep 3 '18 at 13:39











  • Try to get like echo $address->getStreet1(); and echo $address->getStreet2();.

    – Mohit Kumar Arora
    Sep 3 '18 at 13:47











  • @ mohit, tried that one, i didn't get it.

    – jafar pinjar
    Sep 3 '18 at 13:51











  • Have you tried print_r($address->getStreet());

    – Mohit Kumar Arora
    Sep 3 '18 at 13:52












  • Yes, its not printing anything after using this code

    – jafar pinjar
    Sep 3 '18 at 14:17

















0















I am using "customer_address_save_after" event.



This is my observer code.



public function execute(MagentoFrameworkEventObserver $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










share|improve this question


























  • Can you please add the error message in your question?

    – Mohit Kumar Arora
    Sep 3 '18 at 13:39











  • Try to get like echo $address->getStreet1(); and echo $address->getStreet2();.

    – Mohit Kumar Arora
    Sep 3 '18 at 13:47











  • @ mohit, tried that one, i didn't get it.

    – jafar pinjar
    Sep 3 '18 at 13:51











  • Have you tried print_r($address->getStreet());

    – Mohit Kumar Arora
    Sep 3 '18 at 13:52












  • Yes, its not printing anything after using this code

    – jafar pinjar
    Sep 3 '18 at 14:17













0












0








0








I am using "customer_address_save_after" event.



This is my observer code.



public function execute(MagentoFrameworkEventObserver $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










share|improve this question
















I am using "customer_address_save_after" event.



This is my observer code.



public function execute(MagentoFrameworkEventObserver $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







magento2 event-observer customer-address






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 3 '18 at 13:46







jafar pinjar

















asked Sep 3 '18 at 12:04









jafar pinjarjafar pinjar

1,0604 silver badges25 bronze badges




1,0604 silver badges25 bronze badges















  • Can you please add the error message in your question?

    – Mohit Kumar Arora
    Sep 3 '18 at 13:39











  • Try to get like echo $address->getStreet1(); and echo $address->getStreet2();.

    – Mohit Kumar Arora
    Sep 3 '18 at 13:47











  • @ mohit, tried that one, i didn't get it.

    – jafar pinjar
    Sep 3 '18 at 13:51











  • Have you tried print_r($address->getStreet());

    – Mohit Kumar Arora
    Sep 3 '18 at 13:52












  • Yes, its not printing anything after using this code

    – jafar pinjar
    Sep 3 '18 at 14:17

















  • Can you please add the error message in your question?

    – Mohit Kumar Arora
    Sep 3 '18 at 13:39











  • Try to get like echo $address->getStreet1(); and echo $address->getStreet2();.

    – Mohit Kumar Arora
    Sep 3 '18 at 13:47











  • @ mohit, tried that one, i didn't get it.

    – jafar pinjar
    Sep 3 '18 at 13:51











  • Have you tried print_r($address->getStreet());

    – Mohit Kumar Arora
    Sep 3 '18 at 13:52












  • Yes, its not printing anything after using this code

    – jafar pinjar
    Sep 3 '18 at 14:17
















Can you please add the error message in your question?

– Mohit Kumar Arora
Sep 3 '18 at 13:39





Can you please add the error message in your question?

– Mohit Kumar Arora
Sep 3 '18 at 13:39













Try to get like echo $address->getStreet1(); and echo $address->getStreet2();.

– Mohit Kumar Arora
Sep 3 '18 at 13:47





Try to get like echo $address->getStreet1(); and echo $address->getStreet2();.

– Mohit Kumar Arora
Sep 3 '18 at 13:47













@ mohit, tried that one, i didn't get it.

– jafar pinjar
Sep 3 '18 at 13:51





@ mohit, tried that one, i didn't get it.

– jafar pinjar
Sep 3 '18 at 13:51













Have you tried print_r($address->getStreet());

– Mohit Kumar Arora
Sep 3 '18 at 13:52






Have you tried print_r($address->getStreet());

– Mohit Kumar Arora
Sep 3 '18 at 13:52














Yes, its not printing anything after using this code

– jafar pinjar
Sep 3 '18 at 14:17





Yes, its not printing anything after using this code

– jafar pinjar
Sep 3 '18 at 14:17










2 Answers
2






active

oldest

votes


















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;






share|improve this answer

























  • how this can be used in observer?

    – jafar pinjar
    Sep 3 '18 at 12:35











  • I am getting error, if i use this code also echo $address->getStreet();

    – jafar pinjar
    Sep 3 '18 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.

    – Naveed Asim
    Sep 3 '18 at 12:48











  • It would be helpful, if you update me the code

    – jafar pinjar
    Sep 3 '18 at 12:50


















0














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(MagentoFrameworkEventObserver $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();






share|improve this answer



























  • Yes it is coming in two lines not as array, it is coming as string

    – jafar pinjar
    Sep 5 '18 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? :)

    – cygnus digital
    Sep 5 '18 at 13:48












  • @jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

    – cygnus digital
    Sep 5 '18 at 15:07














Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f240621%2faddress-update-get-street-problem-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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;






share|improve this answer

























  • how this can be used in observer?

    – jafar pinjar
    Sep 3 '18 at 12:35











  • I am getting error, if i use this code also echo $address->getStreet();

    – jafar pinjar
    Sep 3 '18 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.

    – Naveed Asim
    Sep 3 '18 at 12:48











  • It would be helpful, if you update me the code

    – jafar pinjar
    Sep 3 '18 at 12:50















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;






share|improve this answer

























  • how this can be used in observer?

    – jafar pinjar
    Sep 3 '18 at 12:35











  • I am getting error, if i use this code also echo $address->getStreet();

    – jafar pinjar
    Sep 3 '18 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.

    – Naveed Asim
    Sep 3 '18 at 12:48











  • It would be helpful, if you update me the code

    – jafar pinjar
    Sep 3 '18 at 12:50













0












0








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;






share|improve this answer













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;







share|improve this answer












share|improve this answer



share|improve this answer










answered Sep 3 '18 at 12:34









Naveed AsimNaveed Asim

2,8132 gold badges3 silver badges17 bronze badges




2,8132 gold badges3 silver badges17 bronze badges















  • how this can be used in observer?

    – jafar pinjar
    Sep 3 '18 at 12:35











  • I am getting error, if i use this code also echo $address->getStreet();

    – jafar pinjar
    Sep 3 '18 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.

    – Naveed Asim
    Sep 3 '18 at 12:48











  • It would be helpful, if you update me the code

    – jafar pinjar
    Sep 3 '18 at 12:50

















  • how this can be used in observer?

    – jafar pinjar
    Sep 3 '18 at 12:35











  • I am getting error, if i use this code also echo $address->getStreet();

    – jafar pinjar
    Sep 3 '18 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.

    – Naveed Asim
    Sep 3 '18 at 12:48











  • It would be helpful, if you update me the code

    – jafar pinjar
    Sep 3 '18 at 12:50
















how this can be used in observer?

– jafar pinjar
Sep 3 '18 at 12:35





how this can be used in observer?

– jafar pinjar
Sep 3 '18 at 12:35













I am getting error, if i use this code also echo $address->getStreet();

– jafar pinjar
Sep 3 '18 at 12:36





I am getting error, if i use this code also echo $address->getStreet();

– jafar pinjar
Sep 3 '18 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.

– Naveed Asim
Sep 3 '18 at 12:48





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.

– Naveed Asim
Sep 3 '18 at 12:48













It would be helpful, if you update me the code

– jafar pinjar
Sep 3 '18 at 12:50





It would be helpful, if you update me the code

– jafar pinjar
Sep 3 '18 at 12:50













0














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(MagentoFrameworkEventObserver $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();






share|improve this answer



























  • Yes it is coming in two lines not as array, it is coming as string

    – jafar pinjar
    Sep 5 '18 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? :)

    – cygnus digital
    Sep 5 '18 at 13:48












  • @jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

    – cygnus digital
    Sep 5 '18 at 15:07
















0














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(MagentoFrameworkEventObserver $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();






share|improve this answer



























  • Yes it is coming in two lines not as array, it is coming as string

    – jafar pinjar
    Sep 5 '18 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? :)

    – cygnus digital
    Sep 5 '18 at 13:48












  • @jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

    – cygnus digital
    Sep 5 '18 at 15:07














0












0








0







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(MagentoFrameworkEventObserver $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();






share|improve this 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(MagentoFrameworkEventObserver $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();







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 5 '18 at 13:52

























answered Sep 5 '18 at 13:35









cygnus digitalcygnus digital

2473 silver badges15 bronze badges




2473 silver badges15 bronze badges















  • Yes it is coming in two lines not as array, it is coming as string

    – jafar pinjar
    Sep 5 '18 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? :)

    – cygnus digital
    Sep 5 '18 at 13:48












  • @jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

    – cygnus digital
    Sep 5 '18 at 15:07


















  • Yes it is coming in two lines not as array, it is coming as string

    – jafar pinjar
    Sep 5 '18 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? :)

    – cygnus digital
    Sep 5 '18 at 13:48












  • @jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

    – cygnus digital
    Sep 5 '18 at 15:07

















Yes it is coming in two lines not as array, it is coming as string

– jafar pinjar
Sep 5 '18 at 13:38





Yes it is coming in two lines not as array, it is coming as string

– jafar pinjar
Sep 5 '18 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? :)

– cygnus digital
Sep 5 '18 at 13:48






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? :)

– cygnus digital
Sep 5 '18 at 13:48














@jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

– cygnus digital
Sep 5 '18 at 15:07






@jafarpinjar did any of this help? Did you get any output from my last piece of debugging code??

– cygnus digital
Sep 5 '18 at 15:07


















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f240621%2faddress-update-get-street-problem-magento-2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?