We are going to be creating a booking form that dynamically populates the price of the ticket from a custom post type and then keeps track of how many tickets are available. It will stop people booking more than the available tickets and give them the appropriate error.
Demo
Setup
1. Create a new post type
I am using ACF Extended to create my post types, but CPTUI plugin would also work.
2. Create Event Price field
We need to create a number field to store the price of the event.
Set the field_name to event_price
3. Create Event Tickets field
We need to create a number field to store the number of tickets for the event.
Set the field_name to event_tickets
4. Add a new event and populate fields
5. Create a new form
Add two number fields: event_price and event_tickets
The fields need to have the name attributes set under the advanced tab as event_price
and event_tickets
respectively.
6. Post ID field
Add a field to store the post ID of the event.
Make sure you set the default value to {embed_post.ID}
7. Create your Event template
I am using GeneratePress & GenerateBlocks in this example. However you can use whatever builder or theme you like.
The only important thing is that you add the Fluent Form you just created somewhere inside the template, using the supported Gutenberg block or shortcode method.
The Code
Copy and paste the code below into your functions.php or code snippet plugin.
Make sure you update the Form ID's
to your form