Tutorial: Use merge fields to pass users address to MailChimp via API without empty result. Problem solved
Recently I embarked on a journey to integrate MailChimp API with abandoned carts plugin. At first I thought that it should be a peace of cake since I saw their huge documentation, but I was mistaken and had to spend my whole afternoon digging google to understand why I am getting empty address field after everything else is imported. Just to be clear – I really like the new Mailchimp 3.0 API and they have done a beautiful job on the documentation but there still are some blind-spots.
From MailChimp documentation I found that when you are trying to add a new subscriber to one of your mailing lists you can use the “merge_fields” parameter to pass user related information like Name, Last name, Phone number and Address (this merge_fields cheat-sheet also came in handy). Passing all of these fields was quite easy except for Address field which didn’t register on MailChimp and every time returned an empty result (see screenshot below).
I tried multiple and various solutions to get this problem fixed (passing arrays, objects, strings etc.) until I turned to Google but it was really hard to find a good article that would help to fix this issue or explain how to pass Address field to MailChimp using merge_fields until I found a published email conversation that gave some explanation into how MailChimp is accepting Address field.
If you want to prevent Mailchimp returning empty Address field you have to pass 5 mandatory fields to it:
- Address Line 1 (addr1)
- City (city)
- State (state)
- Zip code (zip)
- Country (country)
Unless one of these is left out, you will see empty column. Also I must add that none of these fields can be left blank even if you do not need or have some of those values replace it with “n/a” or similar.
Here is the working solution and end result of passing Address field to MailChimp via API:
$data = array( 'email_address' => 'email@example.com', 'status' => 'subscribed', 'merge_fields' => [ 'FNAME' => 'Daffy', 'LNAME' => 'Duck', 'ADDRESS' => array( 'addr1' => '4000 Warner Boulevard', 'city' => 'Burbank', 'state' => 'California', 'zip' => '91522', 'country' => 'US' ) ] ); $mailchimp_url = 'https://us2.api.mailchimp.com/3.0/'; $list_id = '73abcDe'; $result = $this->connect($mailchimp_url. 'lists/'.$list_id.'/members', 'POST', $mailchimp_api_key, $data);
After this I was finally able to get Address fields over to MailChimp. I hope this tutorial helps to save time and money for others in the same spot as I found myself one day :). Please comment below if you have some additional information to add.