We have 4 Offices with many assigned ZIP Codes:
- 21079, 22085,.... = Hamburg
- 10115, 10178,.... = Berlin
- 60306, 60312,.... = Frankfurt
- .....,......,.... = Munich
- everything else = Head Office
Is it possible to read out the ZIP Code Input from the Form and send the Mail to the assigned Office dynamically?
It has to be dynamically without a dropdown (on Front End) of all ZIP Codes or Cities.
I tried this without success
// hook into wpcf7_before_send_mail
add_action( 'wpcf7_before_send_mail', 'cf7dynamicnotifications'); // Hooking into wpcf7_before_send_mail
function cf7dynamicnotifications($contact_form) // Create our function to be used in the above hook
{
$submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class
$posted_data = $submission->get_posted_data(); // Get all of the submitted form data
if( $posted_data["plz"] == '21079' ) {
$recipient_email = '[email protected]';
}
elseif($posted_data["plz"] == '22085') {
$recipient_email = '[email protected]';
}
elseif($posted_data["plz"] == '12345') {
$recipient_email = '[email protected]';
}
else {
$recipient_email = '[email protected]';
}
// set the email address to recipient
$mailProp = $contact_form->get_properties('mail');
$mailProp['mail']['recipient'] = $recipient_email;
// update the form properties
$contact_form->set_properties(array('mail' => $mailProp['mail']));
}
EDIT
Thank you for your help. My solution works well, I had problems with my email provider.
Do you know how I can extend the value? I would like to instert more than one value.
For example:
if ( '21079', '21080', '21081' === $posted_data['plz'] ) { $recipient_email = '[email protected]';
This gives me an syntax Error.
Also this didnt worked:
if ( '21079' || '21080' || '21081' === $posted_data['plz'] ) { $recipient_email = '[email protected]';
My Answer:
To check if the ZIP code matches multiple values, you can use the `in_array` function in PHP. Here's how you can modify your code to check for multiple ZIP codes:
php
function cf7dynamicnotifications($contact_form) {
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
$recipient_email = '[email protected]'; // Default recipient
$zip_codes_hamburg = array('21079', '21080', '21081');
$zip_codes_berlin = array('10115', '10178');
$zip_codes_frankfurt = array('60306', '60312');
if (in_array($posted_data['plz'], $zip_codes_hamburg)) {
$recipient_email = '[email protected]';
} elseif (in_array($posted_data['plz'], $zip_codes_berlin)) {
$recipient_email = '[email protected]';
} elseif (in_array($posted_data['plz'], $zip_codes_frankfurt)) {
$recipient_email = '[email protected]';
}
$mailProp = $contact_form->get_properties('mail');
$mailProp['mail']['recipient'] = $recipient_email;
$contact_form->set_properties(array('mail' => $mailProp['mail']));
}
This code will check if the submitted ZIP code matches any of the values in the respective arrays and set the recipient email accordingly. You can add more ZIP codes to the arrays as needed.
Rate this post
3 of 5 based on 7753 votesComments