Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter for body parameter data #137

Merged
merged 2 commits into from
Mar 8, 2024
Merged

Conversation

efc
Copy link
Contributor

@efc efc commented Nov 16, 2023

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 dparker1005 merged commit 631a1fd into strangerstudios:dev Mar 8, 2024
@dparker1005
Copy link
Member

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

  • The filter is renamed to pmpromc_update_audience_members_data
  • The filter now passes an object instead of an array. This means that you should be able to set sync_tags with $data->sync_tags = true

Thank you again for your contribution!

@efc
Copy link
Contributor Author

efc commented Mar 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants