Skip Navigation

[Resolved] Simple booking form

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

Problem:
1. I have two custom post types, "Booking" and "Accommodation", in a one-to-many relationship where many Bookings can be connected to one Accommodation. I also have a Form that creates Booking posts, and I would like hose posts to be connected to the proper parent Accommodation automatically.

2. I need to email the booking to either the admin email if no agent is assigned to that accommodation, or to the agent if there is assigned one. For Agents I have another CPT (Ugynokok) which is connected to the accommodation.

Solution:
1. Add a relationship field to the new Booking Form, and set the value to be the ID of the page being displayed. The syntax will look something like this:

<div style="display:none;">
    <div class="form-group">
        <label>Accommodation Bookings</label>
        [cred_field field='@accommodation-booking.parent' class='form-control' output='bootstrap' select_text='--- not set ---' value='[wpv-post-id item="$current_page"]']
    </div>
</div>

2. This type of conditional logic isn't built in to the email notification system, but you can set up something like this using the PHP APIs. We have cred_notification_recipients to modify the notification recipients on-the-fly and the Post Relationships API toolset_get_related_posts to help you determine if there is an associated Agent.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcode
https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api

This support ticket is created 5 years, 1 month 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
#1342109

Hello

I create a site for tourist accomodations. I want to include a simple booking form on the single page of the accomodation. Nothing complex, only basic fields. I created a CPT for bookings (Foglalasok) and I connected it to accomodations CPT (Szallasok) with one-to-many relationship (may bookings for one accomodation). I created a post form for the bookings CPT. I have two problems:
1. I need to save the booking together with the parent post info (the accomodation it belongs to). So when somebody opens the booking post can see the coonection at the bottom of the edit screen, like I would do it the normal way by wp admin.
2. I need to email the booking to either the admin email if no agent is assigned to that accomodation, or to the agent if there is assigned one. For Agents I have another CPT (Ugynokok) which is connected to the accomodation.

#1342219

1. I need to save the booking together with the parent post info (the accomodation it belongs to). So when somebody opens the booking post can see the coonection at the bottom of the edit screen, like I would do it the normal way by wp admin.
Normally when you create a new post Form for a child post, it will include a parent post select field. Does your Booking Form contain an Accommodation select field? If so, then it will be connected automatically to the selected parent. You can also set the parent post value automatically using the current page post ID, and hide the input field from your Users. Then the parent association will be created automatically.

If your Form does not have a post relationship field, you should add one. Set the default value using the current page's post ID, and hide the field from your Users with CSS. It will look something like this:

<div style="display:none;">
	<div class="form-group">
		<label>Accommodation Bookings</label>
		[cred_field field='@accommodation-booking.parent' class='form-control' output='bootstrap' select_text='--- not set ---']
	</div>
</div>

2. I need to email the booking to either the admin email if no agent is assigned to that accomodation, or to the agent if there is assigned one. For Agents I have another CPT (Ugynokok) which is connected to the accomodation.
This type of conditional logic isn't built in to the email notification system, but you can set up something like this using the PHP APIs. We have cred_notification_recipients to modify the notification recipients on-the-fly and the Post Relationships API toolset_get_related_posts to help you determine if there is an associated Agent: https://toolset.com/documentation/programmer-reference/cred-api/#cred_notification_recipients
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api
Let me know if you need assistance implementing these APIs.

#1343741

Dear Christian

I solved the second part by simplifying the logic a little. The site won't have many Agents so by creating wp users for those and changing the author of the accomodation posts for the agent it belongs to it's easy to send the notification, the send notification to post author is already among the settings of the post form.

As for the first question, the child post creation form (booking) will sit inside the parent post (accomodation). I have the accomodation select possibility but I don't want to add the possibilty to select accomodation. The parent accomodation should be the current post.

I tried to add this at the beginning of my post form:

    <div style="">
    [cred_field field='_wpcf_belongs_szallas_id' value='[wpv-post-id id='$current_page']' select_text='--- not set ---' class='form-control' output='bootstrap']
    </div>

The html output is blank for this field.

I also added a code snippet I found on the forum to functions.php to add a parent title to the child as well:

add_action('cred_save_data', 'copy_parent_title_to_child',10,2);
function copy_parent_title_to_child($post_id, $form_data)
{
  // if a specific form
  if ($form_data['id']=='3193')
  {
    if (isset($_POST['_wpcf_belongs_szallas_id']))
    {
      $my_post = array(
      'ID' => $post_id,
      'post_title' => get_the_title($_POST['_wpcf_belongs_szallas_id'])
      );
     wp_update_post( $my_post );
    }
  }
}
#1343827

I could solve the connecting part like this. I'll just hide with css the select just like you suggested:

		<div class="form-group col-md-12">
			<label>Szállások Foglalások</label>
			[cred_field field='@szallas-foglalas.parent' class='form-control' output='bootstrap' select_text='--- not set ---' value='[wpv-post-id id="$current_page"]']
		</div>

Is there a possibility to show the parent in the wordpress admin for the Booking list as a separate column?

I just have to solve somehow the part to name the booking post as the parent post. Why that code is not working for me? It seems quite staightforward...

#1344035

Since the new Post Relationships system was implemented, the _wpcf_belongs_szallas_id field isn't used for post relationships, so you can't rely on it in your cred_save_data callback. If you want to check the post ID of the selected parent in your cred_save_data callback, you can use this syntax instead:

$_POST['@szallas-foglalas_parent']
#1346107

My issue is resolved now. Thank you Christian!