Skip Navigation

[Resolved] Having a difficult time getting messages between users to work properly

This thread is resolved. Here is a description of the problem and solution.

Problem: On my Listing post page, I have a link to a CRED form page where the User can create a Message (a new post) regarding the Listing. I would like to be able to send a notification email to the author of the Listing when the form is submitted, and I would like to store the Listing author's information in the Message post as a custom field.

Solution:
Capture the correct value for the seller's ID in a generic field, using the URL parameter for the listing:

[cred_generic_field field='seller_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"generic_type":"user_id",
"default":"[wpv-post-author format='meta' meta='ID' id='[wpv-post-param var='listing-id']']"
}
[/cred_generic_field]

In the email notifications editor, select the proper information from the form fields to use as the "To" email address for the seller. In this case, it's seller_id.

Use the cred_save_data hook to update the message post using the seller ID captured in your generic field:

add_action('cred_save_data', 'custom_message_to',10,2);
function custom_message_to($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==387 && isset($_POST)) {
        $seller_id = $_POST['seller_id'];
        update_post_meta( $post_id, 'wpcf-message-to', $seller_id );
    }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

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

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#553326

Support,

I am having a difficult time getting my messages to display the correct outputs.

1. I need the motorcycle title that the message was sent about to appear after in regards in this screenshot: hidden link
Right now it's showing the subject of the message sent

2. Messages aren't making it to the inbox of the seller of the posts, so I'm not sure if the same problem above is appearing for received messages. Not sure why they aren't making to inboxes as I followed the format from the classifieds test site.

Thank you,
Chuck

#553591
Screen Shot 2017-07-27 at 1.07.10 PM.png

1. I need the motorcycle title that the message was sent about to appear after in regards in this screenshot...Right now it's showing the subject of the message sent
I believe this is directly related to the other ticket and the motorcycle-info shortcode. Let's standby on this issue until the other ticket is cleared up.

2. Messages aren't making it to the inbox of the seller of the posts, so I'm not sure if the same problem above is appearing for received messages.
It looks like you need to make a small adjustment to your CRED form. Edit the form that creates Messages, and toggle open your email notification area. In "Where to send this notification" nothing is selected in the To: field, you must select something for the User ID. See the attached screenshot. In the demo Classifieds site, this is the advertiser ID but yours may be different.

#553599

Christian,

Still not making it. I updated the settings as you mentioned. Seller_id was what I had to chose.

Here is a screen shot of sent messages: hidden link

I've tried from multiple user names. They aren't reaching inboxes through the site or through email provider.

Would it be something in the views??

Thanks,
Chuck

#553649

Look here:

[cred_generic_field field='seller_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"generic_type":"user_id",
"default":"[wpv-view name='Seller details view']"
}
[/cred_generic_field]

Two problems with this:
1. The "Seller details view" View does not exist. Either you made a mistake typing the name, or you changed the name of the View after the form was created and did not update the shortcode here.

2. A View will output a lot of extra markup and HTML that will not be allowed here. You should use simple text to supply the default value.

#553973

Christian,

I apologize. I was following the tutorial coding and must have deleted the Seller detail view.

Is it best to remove that code? Will the form work correctly once that code is removed?
Is there another route to take or additional work that should be done to have the messages sent to the seller correctly?

Thanks,
Chuck

#554414

Is it best to remove that code?
Yes, I have removed this code and replaced it with something better. See below.

Will the form work correctly once that code is removed?
No, because you must still pass the seller_id value into this generic field. See below.

Is there another route to take or additional work that should be done to have the messages sent to the seller correctly?
Yes, please take a look at the nested shortcodes I have placed in the default attribute value:
[cred_generic_field field='seller_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"generic_type":"user_id",
"default":"[wpv-post-author format='meta' meta='ID' id='[wpv-post-param var='listing-id']']"
}
[/cred_generic_field]

You need to know the seller's ID. The seller is the same as the author of the motorcycle post, correct? So you can use the wpv-post-author tag to get information about the post's author.
See more information about this shortcode here:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-author

Notice that it accepts an "id" attribute. In order to access the motorcycle post, you need to use the wpv-post-param shortcode I described earlier to pass in the motorcycle ID.

Please try this out. I've added the correct code and made the correct changes in your form, and I see the correct seller ID appearing in the form fields, so I think we're pretty close to having this work.

#554467

I sent a test message, but nothing came through.

Was there anything I needed to do per your last comment?

Thanks,
Chuck

#554477

Quick question - did you navigate directly to the page where the CRED form is placed, or did you click a CRED child form link to get there? If you click a CRED child form link, the URL should have a URL parameter that represents the parent post. If you load the page directly it will not have a URL parameter and this approach won't work.

#554541

This is the page with a button to contact a seller: hidden link

Here's how the URL looks after the contact seller button is clicked: hidden link

It looks like it should work as the ID is present in both URLs, but still nothing is reaching the inboxes.

#554542

BUT if a user is not logged in when they click the contact seller button, they are prompted to login: hidden link

The URL with the login is displayed as this: hidden link

Upon logging in the URL changes to only this: hidden link

So the only way it works correctly is if the user is logged in prior pressing the contact seller button, or they must login on the screenshot page, go back and click contact seller button again for the proper URL to appear.

#554543

I have a request into Beda to try and build a modal instead. This would allow a cleaner approach and perhaps solve the url probelm.

#554720

Okay I will make a clone of your site so I can run some more tests locally. It sounds like you have everything set up correctly for these emails to be sent. Please stand by and I will update you shortly.

#554792
Screen Shot 2017-07-31 at 11.57.08 AM.png

Okay it appears that you do not have any code in place to capture the seller_id from your CRED form and use it to update the 'Message To' custom field in your new Message. So even though the Message is created, and an email notification is sent, the "Message To" field never gets set to the author's ID. This is why the View of Received Messages does not show any results.

You must add some additional code in functions.php that will capture the 'seller_id' generic field and use it to update the 'message-to' field in the new post:

add_action('cred_save_data', 'custom_message_to',10,2);
function custom_message_to($post_id, $form_data)
{
    // if a specific form
    if ($form_data['id']==387 && isset($_POST)) {
        $seller_id = $_POST['seller_id'];
        update_post_meta( $post_id, 'wpcf-message-to', $seller_id );
    }
}
#554986

Thank you Christian! That did the trick.

Do I still need this shortcode in the functions.php:

// Access URL parameters
// Example: [wpv-post-param var='someurlparam']
// =============================================================================
add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );
function wpv_post_param_shortcode( $atts ) {
  if ( !empty( $atts['var'] ) ) {
    $var = (array)$_GET[$atts['var']];
    return esc_html( implode( ', ', $var ) );
  }
}

Thanks,
Chuck

#555211

Yes, you should keep this code. It allows you to use the wpv-post-param shortcode, which is required to set the value of the generic field that captures the seller ID:

[cred_generic_field field='seller_id' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"persist":1,
"generic_type":"user_id",
"default":"[wpv-post-author format='meta' meta='ID' id='[wpv-post-param var='listing-id']']"
}
[/cred_generic_field]