It's common for restaurants to offer online booking services on their websites. In this tutorial, I'll show you how to create an online reservation form. This form allows clients to provide essential information for their reservation, select their desired services or items, and even calculate the total amount right within the form.

An online reservation form

In this practice, I’ll provide you two methods to create this kind of reservation:

  • Method 1: Non-coding. But, use a field named Math Operation from a 3rd-party (still based on Meta Box) for automatically calculating the total amount.
  • Method 2: Using code to calculate the amount.

Video Versions

For the method 1 without coding, you can look for this video:

For the method 2 that use some code, you can look for the following video:

Before Getting Started

These are some tools we need for this practice:

First, we should have the Meta Box framework. It’s available on wordpress.org.

Moreover, we’ll need some advanced features from Meta Box extensions, as follows.

Create a New Custom Post Type

Go to Meta Box > Post Types > New Post Type to create a new one for the reservation.

Create a new custom post type for the reservation

Create Custom Fields for the Reservation Form

Let’s create custom fields to save the reservation information. You can create fields as usual according to your requirements of the booking. Here are some fields I created.

The created custom fields to save the reservation information

For the Typical Fields

I set the ID of the Full Name field as the post title to automatically save the customer name in the title of the post.

 Set ID of the Full Name field as the post title

Since this field will be displayed on a page on frontend, you may want to style the field. So you can add a CSS class for the field. Just go to the Advanced tab in the field settings and name a class in this box.

Add a CSS class for the field

If you want to show all the information of the user's order in the admin column for better management, simply turn on the option shown in the image below.

Show all the information of the user's order in the admin column

Next, I need a field that allows customers to choose a set lunch, so I choose the Select field type.

In the Choices section, we usually input the set lunch name only. But, I’ll do a trick. Instead of creating another field for price, I put the price as the value of the set lunch. And the label of each option will be the set lunch’s name.

Set options with the values for the custom fields of choosing a set lunch

Similar to the Private Room field. I also set the value as price.

Set the value as price for the Private Room field.

For the Amount Field

As I mentioned before, we have two ways to create this field. The difference is just how you make it to calculate following your own formula.

Method 1: Calculate Without Using Code

To make the field calculate following a formula without using any code, you should use a 3rd-party git to have a field type named Math Operation.

Without this field, you need to add some code to the theme files to have a field that can automatically calculate based on the values from other fields. But with this field, you should do nothing, just enter the formula, then it will do everything for you.

To have that field, follow the Github link and download these 3 files.

Download these 3 files to install the Math Operation field

Then, upload them into the corresponding JS and CSS folders and add the following code to the functions.php file to declare them.

function child_theme_scripts() {
    wp_enqueue_style('font-awesome', 'https://cdn.jsdelivr.net/fontawesome/4.7.0/css/font-awesome.min.css', '', '4.7.0');
}
add_action( 'wp_enqueue_scripts', 'child_theme_scripts' );

Then when creating custom fields, you will see a new field type named Math Operation. Just choose it.

A new field type named Math Operation.

In this field, there will be 2 special settings like this.

2 special settings of the Math Operation field

In there:

  • The first setting is to identify the fields on which the calculation will be made. You should add the ID of all the fields which appear in the below formula.
  • The second one is to insert the formula.

That’s done for the Math Operation field. Thanks to this field, there is no need to add code anymore. It will work later without any further action.

Method 2: Calculate Using Code

In the case you want to code to add functions to the Amount field, just create it in the Text field type.

Choose the Text field type for to the Amount field

The value of the Amount field will be $0 as default and will not change no matter what you change the information in the fields or not. To figure out the amount then apply the number into this field, we can use some code as follows.

In the functions.php file, add this code to create the formula.

add_action( 'wp_ajax_total', 'text_domain_load_total_ajax' );
add_action( 'wp_ajax_nopriv_total', 'text_domain_load_total_ajax' );
function text_domain_load_total_ajax() {
    $adults = $_POST['adults'] ?? 1;
    $children = $_POST['children'] ?? 0;
    $set_lunch = $_POST['set_lunch'] ?? 0;
    $private_room = $_POST['private_room'] ?? 0;
    $total = ( $adults * $set_lunch ) + ( $children * $set_lunch /2 ) + $private_room;
    $total = '$' . number_format($total, 2, '.', ',');
    wp_send_json_success( $total );
    die();
}

Explanation:

  • add_action (): is a hook to create an action named total;
  • text_domain_load_total_ajax(): is the function to load the values of all the variables and then put them into the formula;
  • $adults, $children, $set_lunch, $private_room: are the variables;
  • $adults = $_POST['adults'] ?? 1: is to check if the adults variable has any value. If it has, the value will be admitted. Otherwise, this variable will be applied to the default value, in this case, it is 1. You can set the default value as you want;
  • wp_send_json_success( $total ): is to return the value of the total variable in json.

So far, no value has passed to any variable yet. All the variables are 0 or 1 as default value as I set in the above code.

Whenever customers change the options in the fields, the value of the options must be recognized and passed to those variables. To load those values, I need to create a script.js file in the js folder of the theme as follows:

(function($){
    $(document).ready(function(){
        $("#adults, #children, #set_lunch, #private_room").on( 'change', function(){
            var adults = $('#adults').val();
            var children = $('#children').val();
            var set_lunch = $('#set_lunch').val();
            var private_room = $('#private_room').val();
            console.log( set_lunch );
            $.ajax( {
                type: "post",
                dataType: "json",
                url : ReservationParams.ajaxURL,
                data: {
                    action: 'total',
                    adults: adults,
                    children: children,
                    set_lunch: set_lunch,
                    private_room: private_room
                },
                success: function(response) {
                    //console.log(response);
                    $('#amount').val(response.data);
                },
                error: function( jqXHR, textStatus, errorThrown ){
                    console.log( 'The following error occurred: ' + textStatus, errorThrown );
                }
            } )
        } )
    })
})(jQuery)

Create a JS file to load the value and send it to the function

You can see that I used ajax here to load the values. In there:

  • #adults, #children, #set_lunch, #private_room: are the IDs of the custom fields in the reservation form;
  • adults, children,set_lunch, private_room: are the variables using for the above formula, I’m assigning the values of the fields with the corresponding IDs to them;
  • action: 'total': The values are being sent by ajax to this action with the url is url : ReservationParams.ajaxURL;
  • success: function(response): if the request succeeds, this will call the value returned by the functions file then print to the custom field which has the ID as amount.

Now, add this code to the functions.php, inside the child_theme_scripts() function we have added before to enqueue both the script.js file and the url of ajax.

wp_enqueue_script( 'theme-script', get_stylesheet_directory_uri() . '/js/script.js', array( 'jquery' ), '1.0', true );
wp_localize_script( 'theme-script', 'ReservationParams', array( 'ajaxURL' => admin_url( 'admin-ajax.php' ) ) );

Enqueue the JS file and URL of the AJAX in the functions file to enable them

Note:

  • 'theme-script': this name is the one you created, please pay attention that you must set it be the same in both lines;
  • 'ReservationParams' and 'ajaxURL' are from url : ReservationParams'.ajaxURL, you can change them, but do it at both.

That’s all for the fields.

Field Group Settings

After creating all the fields, go to the Settings tab > choose Location as Post type, and select the Reservation to apply these fields to it.

Set location to apply the custom fields

Once you have published the field group, its ID will be generated automatically. Just copy it, since we’ll use it in the next step.

The ID of the field group

Create the Reservation Page

I’ll use a page with the pre-built template, which is quite simple to make a demo for this practice. You can see in the demo below, we have the booking form. It’s built from the custom fields which we have just created.

 The booking form

Create a New Page

Go to Pages, create a new one and choose a template for the page.

Choose a template for the page

Display Form on the Page

Look for the Submission Form block provided by MB Frontend Submission to have the booking form.

The Submission Form block provided by MB Frontend Submission to have the booking form

Once you select this block, it’ll display the Title and Content fields by default.

The default field

If you want to display the custom fields for the reservation form, add the ID of the created field group in the box. Then, you will see all the fields displayed.

Add the ID of the created field group

Select Post Type as Reservation to stipulate that when the user enters data into the form, that data will be saved in the post of the Reservation post type.

Select Post Type as Reservation to stipulate that when the user enters data into the form

In this practice, I’ll not use the Title and Content fields, so hide them by removing the options in the Post Fields section and deleting their labels.

Delete some fields

You also can change the label of the Submit button.

Change the label of the Submit button

Go to the page on frontend, you will see fields displayed like this.

The fields displayed on frontend

Style the Form

I will use CSS to style the form.

Use CSS to style the form

Pay attention that I added a class for each field when creating them. So, just use those classes. I put this code on Github so you can refer to it.

Test the Form

Let’s have a test.

Test the reservation form

You can see in the gif that the Amount field automatically changes the number whenever you change the option on the previous fields. This field is working well no matter what method you use to create the formula.

Then, go to the backend to see if the order is saved or not. You can see everything has been stored in a post already.

The order is saved in backend

Last Words

I have created an order form in two ways, using code or not. Now, it's your turn to choose the method that suits you best. We'd love to see your results, so feel free to share them with us. Thanks for following our blog.

Other case studies you might be interested in

  1. Create A Dynamic Landing Page in WordPress Using Custom Field
  2. Create a Filter to Find Hotels by Location
  3. Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
  4. Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
  5. Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
  6. Create Dynamic Favicon in WordPress using Meta Box plugin
  7. Create Posts Series in WordPress Using Meta Box
  8. Display The Latest Products Section - P2 - Using Meta Box and Elementor
  9. Display The Latest Products Section - P3 - Using Meta Box And Oxygen
  10. How to Add Custom Fields for WooCommerce - P2 - Using MB Views
  11. How to Add Custom Fields to Display Banners using Meta Box Plugin
  12. How to Add Guest Authors and Guest Posts - Using Meta Box
  13. How to Add Related Posts - Using Custom Fields
  14. How to Build a Hotel Booking Website Using Meta Box - P1
  15. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  16. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  17. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  18. How to Create a Classified Ads Website using Meta Box
  19. How to create a FAQs page - P5 - Using Meta Box and Breakdance
  20. How to Create a Product Page - P2 - Using Meta Box and Oxygen
  21. How to Create a Product Page - P3 - Using Meta Box and Bricks
  22. How to Create a Product Page - P4 - Using Meta Box and Elementor
  23. How to Create a Product Page - P5 - Using Meta Box and Gutenberg
  24. How to Create a Product Page - P6 -Using Meta Box and Breakdance
  25. How to Create a Product Page - P7 - Using Meta Box + Kadence
  26. How to Create a Product Page - P8 - Using Meta Box and Brizy
  27. How to Create a Product Page - P9 - Using Meta Box and Divi
  28. How to Create a Product Page using Meta Box Plugin
  29. How to Create a Recipe - P2 - Using Meta Box and Oxygen
  30. How to Create a Recipe - P3 - Using Meta Box and Elementor
  31. How to Create a Recipe - P4 - Using Meta Box and Bricks
  32. How to Create a Recipe - P5 - Using Meta Box and Zion
  33. How to Create a Recipe - P6 - Using Meta Box and Brizy
  34. How to Create a Recipe - P7 - Using Meta Box and Breakdance
  35. How to Create a Recipe - P8 - Using Meta Box and Kadence
  36. How to Create a Recipe - P9 - Using Meta Box and Divi
  37. How to Create a Recipe with Meta Box Plugin
  38. How to Create a Simple Listing - P2 - Using Meta Box and Bricks
  39. How to Create a Simple Listing - P3 - Using Meta Box and Breakdance
  40. How to Create a Simple Listing - P4 - Using Meta Box and Elementor
  41. How to Create a Team Members Page - P1- Using Meta Box and Elementor
  42. How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
  43. How to Create a Team Members Page - P3 - Using Meta Box and Bricks
  44. How to Create a Team Members Page - P4 - Just Meta Box
  45. How to Create a Team Members Page - P6 - using Meta Box and Breakdance
  46. How to Create a Team Members Page - P7 - Using Meta Box and Kadence
  47. How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
  48. How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
  49. How to Create a Video Gallery Page - P4 - Using Meta Box + Elementor
  50. How to Create a Video Gallery Page - P5 - Using MB Views
  51. How to Create a Video Gallery Page - P6 - Using Meta Box and Zion
  52. How to Create a Video Gallery Page Using Meta Box + Oxygen
  53. How to Create ACF Flexible Content Field with Meta Box
  54. How to Create an Auto-Updated Cheat Sheet in WordPress
  55. How to Create an FAQs Page - P1 - Using Meta Box and Elementor
  56. How to create an FAQs page - P2 - Using Meta Box and Oxygen
  57. How to create an FAQs page - P4 - Using Meta Box and Bricks
  58. How to Create an FAQs Page - P6 - Using MB Views
  59. How to Create an FAQs Page - P7 - Using Meta Box and Divi
  60. How to Create an FAQs Page - P8 - Using Meta Box and Kadence
  61. How to Create an FAQs Page -P3- Using Meta Box
  62. How to Create Buttons with Dynamic Link using Custom Fields
  63. How to Create Category Thumbnails & Featured Images Using Custom Fields
  64. How to Create Download and Preview Buttons - P1 - Using Meta Box and Bricks
  65. How to Create Download and Preview Buttons - P2 - Using Meta Box and Oxygen
  66. How to Create Download and Preview Buttons - P3 - Using MB Views
  67. How to Create Download Buttons in WordPress - Using Custom Fields
  68. How to Create Dynamic Landing Page in WordPress - P1 - Using Meta Box and Elementor
  69. How to Create Dynamic Landing Page in WordPress - P2 - Using Meta Box and Bricks
  70. How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
  71. How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
  72. How to Create Notification Using Custom HTML Field
  73. How to Create Online Admission Form for School or University
  74. How to Create Online Reservation Form for Restaurants using Meta Box
  75. How to Create Relationships - P1 - Using Meta Box and Oxygen
  76. How to Create Relationships - P2 - Using Meta Box and Bricks
  77. How to Create Relationships - P3 - Using MB Views
  78. How to Create Relationships - P4 - Using Meta Box and Breakdance
  79. How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
  80. How to Create Taxonomy Thumbnails & Featured Images - P3 - Using Meta Box and Bricks
  81. How to Create Taxonomy Thumbnails & Featured Images - P4 - Using MB Views
  82. How to Create YouTube Video Timestamps on WordPress Website - P1 - Using MB Views
  83. How To Display All Listings On A Map With Meta Box
  84. How to Display Author Bio in WordPress - P1 - Using Meta Box and Bricks
  85. How to Display Author Bio in WordPress - P2 - Using MB Views
  86. How to Display Images from Cloneable Fields - P1 - with Gutenberg
  87. How to Display Images from Cloneable Fields - P2 - Using Meta Box and Oxygen
  88. How to Display Images from Cloneable Fields - P3 - with Elementor
  89. How to Display Images from Cloneable Fields - P4 - with Bricks
  90. How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
  91. How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
  92. How to Display Product Variations - P1 - Using Meta Box and Gutenberg
  93. How to Display Product Variations - P2 - Using Meta Box and Oxygen
  94. How to Display Product Variations - P3 - Using Meta Box and Bricks
  95. How to Display the Dynamic Banners - P2 - Using Meta Box and Bricks
  96. How to Display The Latest Products - P5 - Using Meta Box and Bricks
  97. How to Display the Latest Products - P6 - using Meta Box and Breakdance
  98. How to Display the Latest Products - P7 - Using Meta Box + Kadence
  99. How to Display the Latest Products Section - P4 - Using Meta Box + Zion

Leave a Reply

Your email address will not be published. Required fields are marked *