strangerstudios / pmpro-mailchimp

WordPress plugin for MailChimp integration.
https://www.paidmembershipspro.com/add-ons/pmpro-mailchimp-integration/
GNU General Public License v3.0
25 stars 23 forks source link

Add filter for body parameter data #137

Closed efc closed 8 months ago

efc commented 1 year ago

Sometimes a user of the add-on may need to modify the body parameters of the MailChimp API call. In our case, we need to do this because we are passing along tags to MailChimp and we want these tags to replace any tags already existing on MailChimp. The API allows for this with the sync_tags body parameter, but currently the add-on does not include that parameter. This filter allows us to add the sync_tags parameter when needed.

With this commit in place we can do the following:

/* Add our local tags to each user */
function add_my_tags( $user_data, $user ) {
    $data = get_object_vars( $user_data );
    $data['tags'] = get_my_tags_for_user($user);
    $user_data = (object) $data;
    return $user_data;
}
add_filter('pmpromc_user_data', 'add_my_tags', 10, 2);

/* Make sure that MailChimp will replace its tags with the ones we are sending */
public function sync_my_tags( $data, $audience ) {
    $data['sync_tags'] = true;
    return $data;
}
add_filter('pmpromc_body_parameters', 'sync_my_tags', 10, 2);

Without the sync_tags parameter MailChimp will, by default, only add tags to a list member. With this change we can ask MailChimp to sync these tags for the member. Others may think of further uses for this new filter.

Note that this, in part, would help satisfy this enhancement request for users who are comfortable using filters.

dparker1005 commented 8 months ago

Hi @efc, thank you for this submission! This code is now merged with a couple of tweaks:

Thank you again for your contribution!

efc commented 8 months ago

Thanks, David! Great to see this in the plugin.