I think you’re close. In my code I’ve assumed that the desired customer_email
meta looks like this:
array(
0 => '[email protected]',
1 => '[email protected]',
2 => '[email protected]',
);
What you need to do is add the new email address to the array of existing email addresses, then save the updated array back to the meta value. update_post_meta()
simply overwrites whatever’s already in the meta value, so you can’t use it to simply merge a new value into an existing array.
Something like this should do it:
add_action('wpcf7_mail_sent',
function ($contact_form) {
if ($contact_form->id() === 32009) {
$submission = WPCF7_Submission::get_instance();
$currentdata = get_post_meta(32127, 'customer_email', true);
if ( empty( $currentdata ) ) {
$currentdata = array();
}
// Bail out if $currentdata *isn't* an array.
if ( ! is_array( $currentdata ) ) {
return;
}
$mail = $submission->get_posted_data('email-002');
// Add the new email address to $currentdata.
$currentdata[] = $mail;
update_post_meta(32127, 'customer_email',$currentdata);
}
}
);
Edited: I’ve changed get_post_meta( 32127, 'customer_email' );
to get_post_meta( 32127, 'customer_email', true );
to ensure that we only get a single post_meta value. Otherwise it will grab all the post_meta values with the key customer_email
.
I’m not sure that’s the cause of the error you’re seeing in the comments, but it should make the code more precise.