Skip Navigation

[Resolved] Sending an email to an address affiliated with a "one" in a "one-to-many"

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by Christopher Amirian 1 year, 5 months ago.

Assisted by: Christopher Amirian.

Author
Posts
#2616683

Tell us what you are trying to do?

I have two post types in a one-to-many relationship. On the "one" side, we have "RFQs". On the "many" side we have responses to RFQs.

RFQs are associated with specific countries. For this, I now have it set up so RFQs have a custom multiple checkbox field (I gave up on the taxonomy, but could go back if that works) that lists all the countries. If someone submits a response to an RFQ that is for COUNTRY A, I want it to go to a specific email address associated with COUNTRY A (e.g., mycountry@xxx.com or someweirdtitle@xxx.com).

Is there any documentation that you are following?

I tried a bunch of things:

https://toolset.com/forums/topic/send-email-notification-to-a-user-email-that-has-a-custom-field-with-a-specified/
https://toolset.com/forums/topic/select-include-an-email-recipient-conditionally-of-the-submitted-data/

I think I need to use cred_notification_recipients, but I'm not sure how to put it all together.

Is there a similar example that we can see?

What is the link to your site?
hidden link

#2616735

Christopher Amirian
Supporter

Languages: English (English )

notification1.png
notification2.png

Hi there,

I suggest that you use the custom notification for that.

- Go to edit screen of your form.
- Scroll down to see the "E-mails Notification Settings".
- Add a notification.
- Select a desired name for the notification.
- Choose the "When custom fields values match" option.
- Click the "Add Condition. by Field" button. (check the notification1.png screenshot)
- Select your checkbox field and select the option that you add the notification. (You will have to add a separate notification for each country you have as the checkbox)
- Next, select the " Send notification to a specific email address:" option.
- Add a custom email address for the country in question. (check the notification2.png screenshot)
- Add the rest of the data for the email, including the subject and the content of the email.
- Save the result.
- Repeat the process for the other countries and create a separate notification for each country.

Thanks.

#2616831

Thank you for this info! It is very helpful. However, since the "country" is associated with the "parent" - RFQ - the field I need is not showing up as an option in the options for "When custom field values match." How do I get the Country field from the RFQ that the response was submitted for?

I've thought of a few ways to do this, but I'm not sure of the best. Is there a way to attach the country from the RFQ to the individual RFQ responses somehow? I was able to SHOW the country on the RFQ response form here: hidden link

Can I save that as part of the RFQ response somehow?

Barb

#2616983

Christopher Amirian
Supporter

Languages: English (English )

Hi Barb,

I think it is possible to get the country from the parent ID as you have it in the URL already.

But I'd like to have access to the website to see if I can come up with something or not.

The idea that I have in mind:

- Come up with a way to add the URL query string to a generic field in the form that is hidden.
- Use cred_notification_recipients to setup a notification.
- Inside the callback function of that hook, use the hidden generic field to get the ID and use get_posy_meta() to detect the country:
https://developer.wordpress.org/reference/functions/get_post_meta/
- Send the notification to the correct country email that can be set inside the code as an if/else statements.

Would you please create a complete backup of your website and give the login information by setting the next reply as private?

Also please tell me where to see the Custom fields for RFQs, where can I see the Form edit and which page you added the form in.

Thanks.

#2617005

Custom Fields for RFQs and the RFQs: hidden link

#2617025

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Here is how I managed to handle the issue:

- Added a generic hidden field in the form.
- I used the same method you used to add the country inside the field default value:

[cred_generic_field type='hidden' field='email-country']
{
"required":0,
"validate_format":0,
"default":"[wpv-post-field name="wpcf-procurement-country" id='[wpv-post-param var="parent_rfq_id"]']"
}
[/cred_generic_field]

- Added a notification in the form settings and called it 'Country Email'.
- It will send an email when the form is submitted.
- I did not add any recipients there as we will do it dynamically via code.
- Added a custom code in Toolset named 'rfqnotification':

https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

- Here is the code:

add_filter('cred_notification_recipients', 'modify_recipients', 10, 4);
function modify_recipients($recipients, $notification, $form_id, $post_id) {
 
  	// Check the form ID to target
  	if ($form_id == 239) {
      // Check notification name matches target notification
      if ( isset($notification['name']) && 'Country Email' == $notification['name'] ) {

          // Get the country name
          $the_name = $_REQUEST['email-country'];

          //Add proper address for each country
          if ($the_name == 'Africa Alliance') {
            $recipients[] = array(
                'to'        =>  'to',
                'address'   => '----'
                );            
          } else {
            $recipients[] = array(
                'to'        =>  'to',
                'address'   => '----',
                );               
          }
      }    
    }

          
    return $recipients;
}

- That will generate dynamic email addresses and you need to update the addresses depending on the country name.

Code description:

It uses the 'cred_notification_recipients' hook to modify the notification recipient:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

It first checks to make sure the correct form ID is targeted, for you case it was 239

Then it checks if there is a notification set up for that form or not and target the notification. I you remember the notification I created was called 'Country Email' and that is used in the if statement to check.

Then we get the value of the generic hidden field that we created in the form using $_REQUEST['email-country'].

Next step is a series of If/else statements that provide a separate email for each country name.

I only added the Africa Alliance one and if you see in the address section I added ---, you just need to replace it with the email address that you want to send to for that country.

Thanks.