Skip Navigation

[Résolu] Email notification to parents post authors on many to many relationship

This support ticket is created Il y a 4 années et 5 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 30 réponses, a 2 voix.

Dernière mise à jour par Minesh Il y a 4 années et 5 mois.

Assisté par: Minesh.

Auteur
Publications
#1662985

Hi, I've made a test on a new server.
This is the recap.
Posts type:

  • Post type 1: Ricambisti
  • Post type 2: Case automobilistiche
  • Post type 3: Contatti generici ricambi

Relationship between post type 1 and post type 2: many to many (post type 1 is parent, post type 2 is child):

  • slug: 'ricambista-casa-automobilistica'

Relationship between post type 2 and post type 3: one to many (post type 2 is parent, post type 3 is child).

  • slug: 'marca-contatto-generico-ricambio'

Now I have a post form that creates a new post (a new post of the post type 3) where I have a field for selecting the parent (post type 2) for the post being created.
I want to send the notification email to all the authors of the parents (post type 1) of the parent (post type 2) when a new post is created.

I'll trigger the function only when the notification's name is 'Notifica ricambisti'

add_filter('cred_notification_recipients', 'ts_cred_notification_recipient_19', 10, 4);
function ts_cred_notification_recipient_19($recipients, $notification, $form_id, $post_id){
      
$which_notifications = array( 'Notifica ricambisti' ); // which notifications this applies to
  
// Check notification name matches target notification
if ( isset( $notification['name'] ) && in_array( $notification['name'], $which_notifications ) ) {

I'll get the ID of the parent post (case automobilistiche) from the relationship between post type 2 and post type 3

$case_automobilistiche_id = toolset_get_related_post($post_id, 'marca-contatto-generico-ricambio' );

I'll extract the post objects from the relationship between post type 1 and post type 2

        $ricambisti = toolset_get_related_posts(
            $case_automobilistiche_id,
            'ricambista-casa-automobilistica',
            'child',
            100000,
            0,
            array(),
            'post_object',
            'parent'
            );

And extract the authors email of the post type 1 related to the post type 2 which is related to the post type 3

				// Start with an empty list of email addresses and empty list of post author IDs
                $emails = array();
                $authors = array();
                   
                // Loop over each parent post and get the post authors
                foreach ($ricambisti as $ricambista) {
                   
                $authors[] = get_post_field ('post_author', $ricambista);
                }
                   
                // Ignore duplicates
                $authors = array_unique( $authors );
                   
                // Get email address of authors and create comma separated list
                foreach ($authors as $author) {
                 $emails[] = get_the_author_meta( "user_email", $author );
                }
 
                //error_log(print_r($emails, true));
 
                  // Add recipients by BCC
                if (!empty($emails)) {
                $recipients[] = array(
                'to' => 'bcc',
                'address' => join(",",$emails),
                'name' => '',
                'lastname' => ''
                );
                }
                //error_log("recipients---".print_r($recipients, true));
    }       
    return $recipients;
}

If you want I can share with you FTP and admin credentials, so you can check.

#1663001

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

The main thing I would like to know is - do you see the hook "cred_notification_recipients" is triggered when you submit the form?

#1664311

No, it isn't triggered.
But only with this function..

#1666663

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I just tested with my test site and added the following hook to custom code section:


function tssupp_custom_form_recipients1( $recipients, $notification, $form_id, $post_id ){
  
error_log("MInesh:  cred_notification_recipients: this is triggered successfully at time " . date("d-m-Y H:i:s") );
  
  
   $recipients[] = array(
                'to' => 'to',
                'address' => "xxx@gmail.com",
                'name' => '',
                'lastname' => ''
                );
  
  
    return $recipients;
}
add_filter( 'cred_notification_recipients', 'tssupp_custom_form_recipients1', 10, 4);

And I can confirm that when I submit the form I can see the line added to error log:

"MInesh:  cred_notification_recipients: this is triggered successfully at time " . date("d-m-Y H:i:s")

And also the notification send to xxx email.

If you would like to check I can share the access details to my test site.

Then, I've added the same code I tried with my test site to your site's custom code section and also activated the debug but when I submit the form I do not see any error log entry added to error log file nor the email sent.

This certainly looks like issue from server. Do you have any other test site hosted on another server where I can run a test?

#1666891

Ok, could you please share the access details to your test site.
Thank you.

#1666893

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Sure.
URL: lien caché
user: demo
pwd: Admin@999

The form is available here:
=> lien caché

You can see the notification settings and edit the form here:
=> lien caché

You can find the hook added with the following link and you can change the email to your desired email:
=> lien caché

Let me know if you need anything.

#1666991

Is there a way to have toolset in English and not translated in Italian?

#1667033

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Do you mean in admin you want to display Toolset menu in English - if yes, what if you try to change your site language from Settings => General => Site Language and chose your site language.

#1667199

Hi, I've tested on a new server.
The email is sent and the error is printed on the error_log file.
At this point, I think I'm not correctly triggering the function to send the email notification to the authors of the parent post.

#1667835

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Great - so finally we have a place where we can add the correct code.

Can you please send me access details of your server where the notification works. As well as share the page where you added the form.

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

#1668007

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

When I edit the page where you added the form: lien caché

I can see the two post relationship available - to which post relationship post author you want to send the email, Can you please clarify and once you share this information I will adjust to notification hook code accordingly.
=> lien caché

Also, I see you added two code snippet for notification hook, which I should use and adjust the code?

#1668133

I'd like to send the email to the author of the post type "Ricambisti".

The hook is this one: notifica-ai-ricambisti

#1668163

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

It works as expected I can see now.

I've added the new user test-notification-user1 and test-notification-user2.

Then, I've changed the post author of the following related posts:
=> lien caché - author: test-notification-user1
=> lien caché - author: test-notification-user2

And then submit the form:
=> lien caché

I received the notification to both post author's email for the author "test-notification-user1" and "test-notification-user2".

Can you please confirm now it works as your end, you should check spam folder as well for email. You can change the post author for the post and test it.

#1668167

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I've also set the my email address to section "Send notification to a specific email address: " to the notification "Notifica ricambisti", you should also change the email there as well.
=> lien caché

#1668171

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Please remove the users I've added test-notification-user1 and test-notification-user2 once the testing is complete. 🙂