Skip Navigation

[Closed] Show fields from related post types in form notifications email

This support ticket is created 3 years 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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 12 replies, has 2 voices.

Last updated by Minesh 2 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#2260409

Hi there,

I have a form and notification setup. The body of the email notication includes some fields from related post types. As I learned related items are only working in a limited way in newest toolset.

1) If notifications are sendout right away when creating posts using forms, the right context is given and mail is being sent with field values from related post types.
2) If an event is used to change the status of a post and this is used as reason to trigger a mail notification (i.e. expiration), the right context is not given any more. Only items from the post itself are displayed, items from related post types are not shown.

As I learned, this is expected behaviour (https://toolset.com/errata/post_parent_title-placeholder-doesnt-work-in-cred-notification-emails/).

If there is still no other way, could you please assist me with the snippet from Nigel, as I am getting a 500 when sending a testmail using below code. I am only a bit familiar with php.
Link: https://toolset.com/forums/topic/cred-form-notification-not-showing-parent-post-fields/#post-1842125

My structure:
- My form creates a post from posttype "Checkin".
- It is set to be published upon creation. An expiration date is set and 1 hour before it is met, a notication is being send.
- The notification body includes fields from post type "Raumbuchung".

add_shortcode('related', function ($atts = []) {
 
    // provide defaults
    $atts = shortcode_atts(
        array(
            'id' => null,
        ),
        $atts
    );
 
    $output = "";
 
    if (isset($atts['id'])) {
         
        // logic goes here to get fields from related post
        // here I'm just returning the ID of the current post itself

$checkin= toolset_get_related_post( $raumbuchung_id, array( 'checkin', 'raumbuchung' ) );

        $output = $atts['id'];
    }
 
    return $output;
});

Form body:

[related id="%%POST_ID%%"]
<!-- field from related post type is following -->
[types field='raumbuchung_layout' item='@raumbuchung-checkin.parent'][/types] 

Many thanks
Bernhard

#2260665

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL and admin access details and tell me what notification you setup and as I understand you want to display the field "raumbuchung_layout" that belongs to parent post type with notification body - right?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2262783

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to add the following shortcode to your theme's functions.php file:

add_shortcode('related', function ($atts = []) {
  
    // provide defaults
    $atts = shortcode_atts(
        array(
            'id' => null,
             'field' =>'',
             'relationship' =>'',
        ),
        $atts
    );
  
    $output = "";
  
    if (isset($atts['id'])) {
          
         $child_id = $atts['id'];
         $parent_id = toolset_get_related_post($child_id, $relationship);

            if(!empty($atts['field'])) {
                          return   get_post_meta($parent_id,'wpcf-'.$atts['field'],true);

            }
 
        }
  
    return $output;
});

And then instead of using Types shortcode within notification body:

Raumlayout: [types field='raumbuchung_layout' item='@raumbuchung-checkin.parent'][/types]
Personenanzahl: [types field='raumbuchung_personenbis' item='@raumbuchung-checkin.parent'][/types]
Getränke: [types field='raumbuchung_getranke' item='@raumbuchung-checkin.parent'][/types]
Mittagessen: [types field='raumbuchung_mittagessen' item='@raumbuchung-checkin.parent'][/types]
Belegte Brötchen: [types field='raumbuchung_belegtebrotchen' item='@raumbuchung-checkin.parent'][/types]
Obst: [types field='raumbuchung_obst' item='@raumbuchung-checkin.parent'][/types]
Kuchenbuffet: [types field='raumbuchung_kuchenbuffet' item='@raumbuchung-checkin.parent'][/types]

You should try to use the following way to display related parent post custom fields using the shorcode we created:

Raumlayout: [related   id="%%POST_ID%%"   field="raumbuchung_layout" relationship="raumbuchung-checkin"]
Personenanzahl: [related   id="%%POST_ID%%"   field="raumbuchung_personenbis" relationship="raumbuchung-checkin"]
Getränke: [related   id="%%POST_ID%%"  field="raumbuchung_getranke" relationship="raumbuchung-checkin"]
Mittagessen: [related  id="%%POST_ID%%"  field="raumbuchung_mittagessen" relationship="raumbuchung-checkin"]
Belegte Brötchen: [related  id="%%POST_ID%%"  field="raumbuchung_belegtebrotchen" relationship="raumbuchung-checkin"]
Obst: [related  id="%%POST_ID%%"  field="raumbuchung_obst" relationship="raumbuchung-checkin"]
Kuchenbuffet: [related id="%%POST_ID%%"  field="raumbuchung_kuchenbuffet" relationship="raumbuchung-checkin"] 

To test this again you should try to create a new post using Toolset form and check if you get correct fields in the notification area.

#2262925

Hi Minesh,
thanks. I also registered the shortcode and created a new post using the toolset form.
However, something seems to be not working with the shortcode.
Mail is not being send when [related id=""...] shortcode is used in mailtemplate. I tried it directly in mailbody of notification and within a content template - exactly as you have posted. In addition, when doing mailtest, still gives 500 error.
Could you please take a look again.
Many thanks
Bernhard

#2263321

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should not do a mail test. As when you do mail test the libraries we used to get related posts will not be available and that is why you get the 500 error.

You should create a brand new post and set somehow to CRON run every minute and set your post also to be expired in 1-2 minutes. So you can check it frequently.

Or you can share the link from where I can create a new post and I would like to test it first.

#2263991

Yes I did create new posts. No email was sent so far. When I remove the shortcode in functions.php emails are being send. So I suspect it is related to the shortcode somehow.

You may also create a brand new post by going to this URL
Site Domain + path: /wizard-raumbuchung-5/?raumid=Clara&kundeid=10097&zeitbeginn=1641829500&zeitende=1641830340&reservierungId=10100&raumbuchungid=10103&catering=
You may fill the field Expiration Date by changing URL param "zeitbeginn".

Click on button "Einplanen" to create a new post. The success message shows you the new post ID. You may then change the expiration date in the new post. Toolset Cron settings is set to run every 5 minutes, and in general mails are triggered. Time on server is UTC+1.

#2264501

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please remove the existing "related" shortcode and instead try to use the following shortcode:

add_shortcode('related', function ($atts = []) {
   
    // provide defaults
    $atts = shortcode_atts(
        array(
            'id' => null,
             'field' =>$atts['field'],
             'relationship' =>$atts['relationship'],
        ),
        $atts
    );
   
    $output = "";
   
    if (isset($atts['id'])) {
           
         $child_id = $atts['id'];
         $parent_id = toolset_get_related_post($child_id, $atts['relationship']);
 
            if(!empty($atts['field'])) {
                          return   get_post_meta($parent_id,'wpcf-'.$atts['field'],true);
 
            }
  
        }
   
    return $output;
});

Then, you should try to create a new post and check the notification should work.

#2264581

The mail is now being send, but fields are still empty.

#2264605

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see the fields you are trying to display does not seems to created using Types plugin.

So, Can you please try to find the following line of the code within the 'related' shortcode:

return   get_post_meta($parent_id,'wpcf-'.$atts['field'],true);

And replace the above line of the code with the following line:

return   get_post_meta($parent_id,$atts['field'],true);

As you can see we removed the 'wpcf-'. Once you do that then you should create brand new post again and check if you see notification working as expected and see the custom field values in the email.

#2264749

Replaced the line in my shortcode.

Email being send, but fields are still empty.
I also tried the option to send the email right on submit, but fields are also empty.

#2265283

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As you can see with the following screenshot: hidden link

I can see now that the custom fields does stored with 'wpcf-' prefix. So we need to use the original shortcode that I shared with the following reply:
=> https://toolset.com/forums/topic/show-fields-from-related-post-types-in-form-notifications-email/#post-2264501

add_shortcode('related', function ($atts = []) {
    
    // provide defaults
    $atts = shortcode_atts(
        array(
            'id' => null,
             'field' =>$atts['field'],
             'relationship' =>$atts['relationship'],
        ),
        $atts
    );
    
    $output = "";
    
    if (isset($atts['id'])) {
            
         $child_id = $atts['id'];
         $parent_id = toolset_get_related_post($child_id, $atts['relationship']);
  
            if(!empty($atts['field'])) {
                          return   get_post_meta($parent_id,'wpcf-'.$atts['field'],true);
  
            }
   
        }
    
    return $output;
});

I already run a test on my test install and I can see the above code works fine to get parent ID and based on the parent ID I successfully get the fields belongs to parent and it gets displayed in my notification email.

Can you please try to give another try and try to add the above shortcode within the functions.php file instead of "Custom Code" section offered by Toolset. For now, you can remove the shortcode from custom code section and try to add it to functions.php file and give another try. Create brand new post and make sure you connected the parent which does have all field values.

#2266039

When putting the code to function.php instead of custom code section in toolset, fields are displayed now when using option to send notification right on submit.
When using deferred notifications (send notification on expiration date), fields are still blank.

#2268083

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

On my test install, that is not the case. I can see the correct parent fields are displayed with the post expiration email that is send to before XX time of post expiration.

As shared before, please make sure that you have parent custom fields filled. Otherwise you can setup a test install, a staging site where I can change the post expiration email settings and time and test there and see what happens on test installation.

The topic ‘[Closed] Show fields from related post types in form notifications email’ is closed to new replies.