Tenemos 4 Oficinas con muchos Códigos Postales asignados:
¿Es posible leer el Código Postal Ingresado desde el Formulario y enviar el Correo a la Oficina asignada de forma dinámica?
Tiene que ser dinámicamente sin un menú desplegable (en Front End) de todos los códigos postales o ciudades.
Intenté esto sin éxito
// 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 = 'office1@xyz.com'; } elseif($posted_data["plz"] == '22085') { $recipient_email = 'office2@xyz.com'; } elseif($posted_data["plz"] == '12345') { $recipient_email = 'office3@xyz.com'; } else { $recipient_email = 'head-office@xyz.com'; } // 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'])); }EDITAR
Gracias por tu ayuda. Mi solución funciona bien, tuve problemas con mi proveedor de correo electrónico.
Sabes como puedo ampliar el valor? Me gustaría insertar más de un valor.
Por ejemplo:
if ('21079', '21080', '21081' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com';
Esto me da un error de sintaxis.
También esto no funcionó:
if ('21079' || '21080' || '21081' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com';
Tu pregunta era casi correcta. El set_properties() necesita pasar toda la matriz (en su caso) $mailProp .
/** * Dynamically Change the recipient. * * @param object $contact_form The contact form 7 contact form object. * @return void */ function cf7dynamicnotifications( $contact_form ) { $submission = WPCF7_Submission::get_instance(); // Create instance of WPCF7_Submission class. $posted_data = $submission->get_posted_data(); // Get all of the submitted form data. // Make sure the field is filled in. if ( isset( $posted_data['plz'] ) ) { if ( '21079' === $posted_data['plz'] ) { $recipient_email = 'office1@xyz.com'; } elseif ( '22085' === $posted_data['plz'] ) { $recipient_email = 'office2@xyz.com'; } elseif ( '12345' === $posted_data['plz'] ) { $recipient_email = 'office3@xyz.com'; } else { $recipient_email = 'head-office@xyz.com'; } // 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 ) ); // Pass the whole array. } }