Replies: 0
Hi,
I added a filter in a plugin to add tags based on selected checkboxes.
I noticed that the mailchimp api for PUT and PATCH does not actually update the Tags.
I did figure a work-around but it is not great…
Would much prefer it if mailchimp-for-wp could update tags for an existing subscription.
Or maybe if an action could be made passing in the api, list_id, subscriber, existing_member_data after the submission?
Also I can’t really remove tags for my use case as the same list is used across multiple forms/sites so it would be great to have an option configurable on the form to retain the current tags or to replace completely when updating.
I sent a support request to Mailchimp about this, seems odd that updating a member list would not update the tags yet it can create the tags initially just fine.
// Adding tags to Mailchimp subscribers.
// This feature requires Mailchimp for WordPress version 4.4 or higher
function mc4wp_extras_form_subscriber_data($subscriber) {
// Get post args _mc4wp_tags[] and set onto $subscriber->tags
if (isset($_POST['_mc4wp_tags'])) {
$additionalTags = array_filter($_POST['_mc4wp_tags'], 'strlen');
foreach ($additionalTags as $tag) {
$subscriber->tags[] = $tag;
}
}
// Note: This work-around is placed below as at the time of writing the MailChimp Patch API did not update Tags on existing member list records.
// Get the MailChimp API wrapper.
$api = mc4wp('api');
// Get the form ID.
$form_id = $_POST['_mc4wp_form_id'];
// Get the form.
/** @var MC4WP_Form $form */
$form = mc4wp_get_form( $form_id );
// Get information on the lists that are selected for the form.
$lists = $form->get_lists();
$email_address = $subscriber->email_address;
// Note: This feature is intended to be used with only 1 list selected
// should multiple lists be selected for a form it will send multiple
// redundant API requests.
$existing_member_data = null;
// loop through lists
foreach ($lists as $list_id) {
try {
// Get existing Member Data.
$existing_member_data = $api->get_list_member( $list_id, $email_address );
} catch(MC4WP_API_Resource_Not_Found_Exception $e) {
// Member does not exist on list continue anyway as this is not a concern the tags will be added
// when the MC4WP plugin adds the email to the list.
continue;
} catch ( MC4WP_API_Exception $e ) {
// Don't care?
continue;
}
$existing_tags = [];
foreach ($existing_member_data->tags as $tag) {
$existing_tags[] = $tag->name;
}
// Merge the tags we are adding into any existing tags and ensure there are no duplicates.
$subscriber->tags = array_unique(array_merge($subscriber->tags, $existing_tags), SORT_REGULAR);
try {
// Build tags object to send to API.
$tags = [];
foreach ($subscriber->tags as $tag) {
$tags[] = [
'name' => $tag,
'status' => 'active'
];
}
// Update the list member tags.
// $subscriber->tags
$api->update_list_member_tags( $list_id, $email_address, [ 'tags' => $tags ] );
} catch(MC4WP_API_Resource_Not_Found_Exception $e) {
// Don't care.
} catch ( MC4WP_API_Exception $e ) {
// Don't care?
//die($e->getMessage());
}
}
return $subscriber;
}
add_filter("mc4wp_form_subscriber_data", "mc4wp_extras_form_subscriber_data");