Events Made Easy › Forums › Tips › Working Integration with Mailchimp using Hooks
- This topic has 0 replies, 1 voice, and was last updated 5 years, 11 months ago by Anonymous.
-
AuthorPosts
-
Tue 8 Jan 2019 at 20:44 #59351AnonymousInactive
For anyone who, like me, has been trying to integrate Mailchimp into EME, here’s some code and tips to help you on your way. This is working on my site (as at Jan ’19). It might also be useful for anyone trying to get started with Hooks.
EME has similar Mailing List functionality to Mailchimp but sometimes familiarity, specific functionality or an historical setup makes it preferable to stick with the two app solution.
The code is mashed together from a lot of sources, apologies I’ve not credited them, I didn’t initially plan to publish this so I didn’t keep track of where the bits came from. If you see any of your code in there – thanks for your help! A lot came from this excellent Forum.
I created a Custom field in EME called “Mailchimp”. It’s a Generic Dropdown field with “Yes” and “No” values. I also have a custom “Company” field of Generic Text which maps to a custom field in Mailchimp called CNAME. On my EME RSVP form I use:
<tr> <th>Would you like to receive our newsletter?</th> <td>#_FIELD{Mailchimp}</td> </tr>
I use a WordPress plugin called My Custom Function to make it easier to add the PHP. Feel free to do it your own way if you know how!
The integration/hook code is below. You will need a little coding ability to spot the bits you need to replace, but hopefully it’s straight forward enough. Don’t just try and use this as-is! This is not so much “colour by numbers” as “colour by simple maths”! I’ve also added some debugging tips. There’s currently no error handling, but errors fail quietly so I’ve not bothered yet, sorry!
/* Whilst testing, turn on debug in in wp_config.php and have it sent to /wp-content/debug.log nano wp-config.php Add/edit: define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); @ini_set('display_errors', 0); sudo service apache2 restart */ /* Debug handler */ if ( ! function_exists('write_log')) { function write_log ( $log ) { if ( is_array( $log ) || is_object( $log ) ) { error_log( print_r( $log, true ) ); } else { error_log( $log ); } } } /* Add a hook for the Events Made Easy system to allow for mailchimp signups */ add_action('eme_insert_rsvp_action', 'my_eme_mailchimp_handler'); function my_eme_mailchimp_handler($booking) { $person = eme_get_person($booking["person_id"]); $answers = eme_get_booking_post_answers($booking["booking_id"]); foreach ($answers as $answer) { if ($answer['field_name'] == "Mailchimp") { $signup = $answer['answer']; } if ($answer['field_name'] == "Company") { $company = $answer['answer']; } // Repeat as above for any custom EME fields. } write_log("my_eme_mailchimp_handler [signup: {$signup}, company: {$company}, fname: {$person['firstname']}, lname:{$person['lastname']}, email:{$person['email']}]"); if ($signup == "Yes") { // Edit these to suit $apiKey = 'somethinglikethis-us11'; // Get this from Mailchimp website. $listId = 'a123456789'; // Get the list ID from "List name and campaign defaults" in the Mailchimp website. $memberId = md5(strtolower($person['email'])); $dataCenter = substr($apiKey,strpos($apiKey,'-')+1); $postData = array( "email_address" => $person['email'], "status_if_new" => 'pending', // use 'subscribed' to add without user verification "merge_fields" => array( "FNAME" => $person['firstname'], "LNAME" => $person['lastname'], "CNAME" => $company // Repeat this line as required for any custom Mailchimp fields. ) ); // If curl_init missing: sudo apt-get install php7.0-curl $ch = curl_init('https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listId . '/members/' . $memberId); curl_setopt_array($ch, array( CURLOPT_CUSTOMREQUEST => "PUT", // Put will create a new entry, or update an existing. CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HTTPHEADER => array( 'Authorization: apikey ' . $apiKey, 'Content-Type: application/json' ), CURLOPT_POSTFIELDS => json_encode($postData) )); $response = curl_exec($ch); write_log("my_eme_mailchimp_handler ["); write_log($response); write_log("]"); }; }
Mailchimp is a pain when it comes to testing. You can only ever sign-up once with an email address, even if you delete it! 🙁 I ended up using a “burner phone” type email address. One option is: https://www.tempmailaddress.com/
I hope this is of some use to someone.
Thanks Franky and the rest of the EME developers for such an awesome plugin!
-
AuthorPosts
- The forum ‘Tips’ is closed to new topics and replies.