Skip Navigation

[Resolved] How to add parent posts custom field to child notification font

This support ticket is created 2 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 9 replies, has 2 voices.

Last updated by Waqar 2 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#2331319
Screenshot 2022-04-01 114618.png

Im trying to use a custom field's value from the parents posts as the email recipient for every form sent using the child's form
im currently following this documentation https://toolset.com/course-lesson/send-notifications-when-someone-submits-the-form/?utm_source=plugin&utm_campaign=forms&utm_medium=gui&utm_term=email-notification

#2331393

Hi,

Thank you for contacting us and I'd be happy to assist.

The shortcodes can be used in the field for the specific email recipient.

You can uncheck the option to send email to the specific email in the notification settings and instead use the "cred_notification_recipients" filter, to include the email from the related parent post:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients

For example, the following snippet will get the ID of the related parent post and then include the email from its custom field in the notification's recipients:


add_filter('cred_notification_recipients', 'add_parent_seller_recipient', 10, 4);
function add_parent_seller_recipient($recipients, $notification, $form_id, $post_id) {

	// Check notification name matches target notification
	if ( isset($notification['name']) && 'Form notification name' == $notification['name'] ) {
		$parent_post_ID = toolset_get_related_post( $post_id, 'relationship-slug', 'parent' );
		if(!empty($parent_post_ID)) {
			$parent_post_email = types_render_field( 'seller-email', array( 'item' => $parent_post_ID, 'output' => 'raw' ) );
			if(!empty($parent_post_email)) {
				$recipients[] = array(
					'to'        =>  'cc',
					'address'   => $parent_post_email,
					'name'      =>  '',
					'lastname'  =>  ''
				);
			}
		}
	}

	return $recipients;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

In the snippet, you'll replace:
- "Form notification name" with the actual name of the form notification
- "relationship-slug" with the actual relationship slug
- "seller-email" with the actual slug of the email field from the parent post

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#2334977
Screenshot 2022-04-06 141333.png
Screenshot 2022-04-06 141144.png
Screenshot 2022-04-06 141019.png

Seller Email is still not receiving email notification from the form. is it possible that i chose the wrong type of relationship?

#2335187

Thanks for writing back and the code changes seem correct.

Can you please share temporary admin login details, along with the link to the page, where this form can be seen?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2339505

Thank you for sharing these details.

During troubleshooting, I noticed that in your email notification "Send To Creator", no email recipient was set.
( screenshot: hidden link )

In this case, as the only recipient is being set up using the custom code, you can change the 'cc' value to "to" in that code snippet:
( screenshot: hidden link )


......
$recipients[] = array(
	'to'        =>  'to',
	'address'   => $parent_post_email,
	'name'      =>  '',
	'lastname'  =>  ''
);
......

This should fix it.

#2340343

I changed it, still im not getting email as seller. i changed the email a couple of times, and its still not working. i tested a couple of times on different forms, its still not working.

Is it because i have the seller email only on the admin side and not shown on the live page? (can always hide it with CSS)

#2340845

During troubleshooting, I noticed the following changes were needed to send the notification to the vehicle's seller email:

1. Your form "Form for Guest Messages", was creating a new "Guest Messages" post, but without setting any relationship with the parent "Vehicles" post.

I've added the relationship field "Guest Messages Vehicles" in the form and set the default value to "[wpv-post-id]" shortcode so that the current "Vehicles" post is automatically selected in the field.
( screenshot: hidden link )

Note: You can include the following custom CSS code in the form, to hide this field, so that visitors can't change the selected vehicle post:


form#cred_form_4719_1_1 .form-group:nth-of-type(6) {
    display: none;
}

2. The name of the second email notification was changed to "send_to_creater", so I updated the same name in the custom code snippet too.
( screenshot: hidden link )

3. In the same email notification, I changed the trigger option to "When submitting the form", from "When the post status changes to published".
( screenshot: hidden link )

After these changes, the second notification is getting sent properly.

Important note: The relationship "Guest Messages Vehicles" is set to be of type "One to One", which means only 1 vehicle post can be connected with only 1 guest message post. The result would be, that if a vehicle is already connected to one guest message it can't be connected to another one and so the subsequent seller's email notifications won't work.

I understand that you'd like to allow multiple messages to be connected to 1 vehicle, so this relationship will need to be changed to type "One to Many" (1 vehicle can connect to multiple guest messages).

#2341905

My issue is resolved now. Thank you so much for all the help, hope this also helps other people looking for the same solution

#2342041
Screenshot 2022-04-15 182933.png

I tried the same code and instruction on a different post type and im getting an error. ill be repeating the same process on different post types as well. should i change anything on the code to make it work on other post types as well?

#2342753

The fatal error is showing because the function name "add_parent_seller_recipient" can't be used again in the new snippet.

To avoid confusion and overlapping of the same function names, you can change the function name "add_parent_seller_recipient" in the existing code snippet for the "guest-message-vehicle" relationship to:


add_filter('cred_notification_recipients', 'add_parent_seller_recipient_vehicle', 10, 4);
function add_parent_seller_recipient_vehicle($recipients, $notification, $form_id, $post_id) {
 
........
   
}

This way, you'll know that this snippet is for the notification between the vehicle and the guest message post type.

Next, suppose you have to add a similar snippet again for the relationship between the classified and the guest message post types.

The new snippet's function name will look like this:


add_filter('cred_notification_recipients', 'add_parent_seller_recipient_classified', 10, 4);
function add_parent_seller_recipient_classified($recipients, $notification, $form_id, $post_id) {
 
........
   
}

This way the function name won't be the same as the one used in the first snippet and you'll also know that it is for the notification between the classified and the guest message post type.

You'll also need to make sure that the form notification name, relationship slug, and the slug of the field for the seller email are updated in the new code snippet as well.

For more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/