How to load an address by street attribute
-
16-10-2019 - |
Question
I can load an address using the following code:
$addresses = $addressObj->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('firstname', $addressData['firstname'])
->addAttributeToFilter('lastname', $addressData['lastname'])
->addAttributeToFilter('city', $addressData['city'])
->addAttributeToFilter('postcode', $addressData['postcode'])
->addAttributeToFilter('telephone', $addressData['telephone'])
->load();
The issue is that street
is an array when returned in the address object so how can I add street
into my filters above?
I know I could load all the addresses and loop through them in code then compare the array with my data but I would like to avoid this step if possible a it's extra overhead.
Solution
This should work:
->addAttributeToFilter('street', 'somewhere');
or this, if you want like
filter
->addAttributeToFilter('street', array('like'=>'%somewhere%'));
Even if the street is returned as an array, in the database is kept as a single field. The values in the array are concatenated by \n
.
If you are looking for an exact address that is on 2 lines this should do it:
->addAttributeToFilter('street', 'somewhere\nelse');
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange