/**
* Intercept CF7 submission to handle dynamic car arrays
*/
add_action( 'wpcf7_before_send_mail', 'dc_autos_merge_car_data' );
function dc_autos_merge_car_data( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
// We look directly at the PHP $_POST because CF7 ignores array names like car_year[]
if ( isset($_POST['car_year']) && is_array($_POST['car_year']) ) {
$years = $_POST['car_year'];
$makes = $_POST['car_make'];
$models = $_POST['car_model'];
$car_html_rows = "";
for ($i = 0; $i < count($years); $i++) {
$count = $i + 1;
// Build the table rows for each car
$car_html_rows .= "
Car #$count
Year:
" . sanitize_text_field($years[$i]) . "
Make:
" . sanitize_text_field($makes[$i]) . "
Model:
" . sanitize_text_field($models[$i]) . "
";
}
// Swap [all-car-details] in the email with our new HTML rows
$mail = $contact_form->prop( 'mail' );
$mail['body'] = str_replace( '[all-car-details]', $car_html_rows, $mail['body'] );
// Save changes back to the form properties
$contact_form->set_properties( array( 'mail' => $mail ) );
}
}
}