How to Create Online Reservation Form for Restaurants using Meta Box
Tags: metabox, wordpress, tutorial
Published: 2021-07-13 16:20:11

It is now so popular that restaurants <strong>allow customers to make online reservation from their website</strong>. So, today, I am going to have a tutorial on how to create an online submission form for that. However, it allows customers to not only provide their information but also <strong>choose a set of food or private room</strong> in advance if they want to. Furthermore, even the total amount will be figured out in the form as well.

<img class="aligncenter" src="https://i.imgur.com/kje0J6I.png" alt="An online reservation form allows customers to make reservations from website of the restaurants" width="1899" height="894" />

But, please note that we do not process the payment in this tutorial. Just show the number to let your customer know the amount they will be charged.

<h2>Before Getting Started</h2>

Along with the Meta Box core plugin from <a href="https://wordpress.org/plugins/meta-box/">wordpress.org</a>, what will we need? It'll be these extensions or just Meta Box AIO from the Developer Bundle or the Lifetime Bundle. They are all available there, in the Meta Box AIO.

<ul>

   <li><a href="https://metabox.io/plugins/custom-post-type/">MB Custom Post Types &amp; Custom Taxonomies</a>;</li>

   <li><a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a>;</li>

   <li><a href="https://metabox.io/plugins/mb-frontend-submission/">MB Frontend Submission</a>;</li>

   <li><a href="https://metabox.io/plugins/meta-box-columns/">Meta Box Columns</a> (optional);</li>

   <li><a href="https://metabox.io/plugins/mb-admin-columns/">MB Admin Columns</a> (optional).</li>

</ul>

<h2>Video Version</h2>

https://youtu.be/K6YfrQ3ZRLY

<h2>Step 1: Create a New Custom Post Type</h2>

Each reservation will be a post in this new post type. So, go to <strong>Meta Box &gt; Post Types &gt; New Post Type</strong> to create a new one.

<img class="aligncenter" src="https://i.imgur.com/r6C0QC6.png" alt="Create a new custom post type for the reservation" width="1920" height="942" />

We've had a video tutorial for post type configuring, please take a look.

https://youtu.be/-oYrHGOri4w

<h2>Step 2: Create Custom Fields for the Reservation Form</h2>

A reservation form usually includes general fields such as name, phone number, email, number of adults and children, date and time, table or room. However, as I said above, my form will have a field for choosing a set of food and show the amount.

<img class="aligncenter" src="https://i.imgur.com/cKFxf8H.png" alt="Create custom fields for the reservation form" width="1920" height="942" />

There are some special and specific settings we should clarify here.

<h3>ID of the Full Name Field</h3>

I want the name of the customer to be the title of the post as well as the reservation, so I set this field's ID as <code>post_title</code>.

<h3>The Options in the Set Lunch and Private Room Fields</h3>

These two fields are Select type, so they have the <strong>Choices</strong> section for entering the options.

<img class="aligncenter" src="https://i.imgur.com/8Iyx98w.png" alt="set options with the values for custom fields of choosing a food set" width="1920" height="942" />

You can see that on the left of the colon (:), I have the numbers. They are the price (USD) of the food set with the corresponding name on the right of the colon.

The same with the <strong>Private Room</strong> field. The price is on the left, and the name of the room is on the right.

<img class="aligncenter" src="https://i.imgur.com/0ZxxVKL.png" alt="set options with the values for the custom fields of choosing a private room" width="1920" height="942" />

<h3>Display Custom Fields as Columns in the Admin Screen</h3>

For management purposes, restaurant owners usually want to see all the reservation information right from the admin screen. Thus, I will display the custom fields of the form as the columns in the <strong>All Reservations</strong> screen.

When we've activated the <a href="https://metabox.io/plugins/mb-admin-columns/">MB Admin Columns</a> extension already, there is a section available in the field's settings that is <strong>Show as an admin column</strong>. Tick it and then some others will appear for further configuration.

<img class="aligncenter" src="https://i.imgur.com/TRcnFBW.png" alt="you must choose a position for the admin column" width="1920" height="942" />

Please be notified that you must choose an option in the <strong>Column position</strong> to complete the setting of the admin column.

Finally, remember to choose the Reservation post type as the Location in the Settings tab of the field group.

<img class="aligncenter" src="https://i.imgur.com/CMCkkRI.png" alt="remember to choose the location of the field group is reservation" width="1920" height="942" />

So now, go to the Reservations menu, you will see the columns as follow:

<img class="aligncenter" src="https://i.imgur.com/Ss8oBsk.png" alt="the custom fields display as the admin columns to show information of the reservations for the restaurant owners" width="1920" height="942" />

<h2>Step 3: Create the Reservation Page</h2>

My Reservation page will have a different look from the others on my website. So, I will create a new template for it. In the <code>template</code> folder of the theme, create a new file named <code>reservation-page.php</code> with this content:

<pre>&lt;?php

/**

* Template Name: Reservation Page

*/

get_header();

if( have_posts() ) :

    while ( have_posts() ) :

        the_post();

        ?&gt;

        &lt;main id="main" class="site-main" role="main"&gt;

            &lt;div class="col-left"&gt;

                &lt;?php the_content(); ?&gt;

                &lt;div id="total1"&gt;&lt;/div&gt;

            &lt;/div&gt;

            &lt;div class="col-right"&gt;

                &lt;?php dynamic_sidebar( 'sidebar-1' );?&gt;

            &lt;/div&gt;

        &lt;/main&gt;

        &lt;?php

        endwhile;

endif;

get_footer();</pre>

In there, <code>sidebar-1</code> is the ID of my pre-made sidebar.

<img class="aligncenter" src="https://i.imgur.com/9YzVQXn.png" alt="using iframe to add Google Maps into sidebar of the reservation page" width="1920" height="942" />

I embed an iframe of Google Maps to pin the restaurant's address on the map as well as add other contact information about the restaurant. It may help customers easier to find the restaurant location and contact them in some special cases.

After applying this template for the reservation page, it has this look:

<img class="aligncenter" src="https://i.imgur.com/vmXkqWs.png" alt="the reservation page with the maps and contact info of the restaurant" width="1920" height="942" />

<h3>Add the Reservation Form to the Page</h3>

After having the <a href="https://metabox.io/plugins/mb-frontend-submission/">MB Frontend Submission</a> extension activated, this shortcode will be auto-generated.

<img class="aligncenter" src="https://i.imgur.com/ddGgKcE.png" alt="MB Frontend submission extension generates the shortcode for the field group" width="1920" height="942" />

It normally is in form of this:

<pre>[mb_frontend_form id='reservation' post_fields='title,content']</pre>

I will remove the attribute <code>post_fields</code> to not display the <strong>Title</strong> and <strong>Content</strong> fields, and as the <code>submit_button</code> attribute as well. Then, paste this code to the content of the reservation page.

<img class="aligncenter" src="https://i.imgur.com/mBfZetu.png" alt="paste the shortcode of the field group to the content of the page to show the field to the reservation page" width="1920" height="871" />

Now, here is the page with the form.

<img class="aligncenter" src="https://i.imgur.com/THoiyeC.png" alt="the reservation form display on the frontend" width="1920" height="1997" />

<h3>Prettify the Reservation Page</h3>

It's a kind of a rough-hewn gem and needs some polish. So, try some CSS.

<pre>#reservation {

  width: 100%; }

.page-template-reservation-page .site-main {

  width: 100%;

  display: grid; }

.no-sidebar .main {

  margin: 0; }

.rwmb-label, .rwmb-input {

  width: 100% !important;

  padding-bottom: 10px; }

.rwmb-input input {

  width: 100%; }

.rwmb-input select {

  width: 100%; }

.page-template-reservation-page .site-main {

  width: 100%;

  display: grid; }

@media all and (min-width: 768px) {

  .page-template-reservation-page .site-main {

    grid-template-columns: 1fr 30%;

    grid-gap: 30px; } }

.eci-info {

  font-size: 16px; }

  .eci-info svg {

    width: 20px !important;

    min-width: 20px !important;

    max-width: 20px !important;

    height: 20px !important; }

.rwmb-input-group-text {

  border: 0 !important; }

.rwmb-button {

  padding: 10px 25px;

  background: #ea0808;

  text-transform: uppercase; }

.calendar .rwmb-input, .adults .rwmb-input, .children .rwmb-input, .full-name .rwmb-input, .email .rwmb-input, .phone .rwmb-input, .book .rwmb-input, .black-tie .rwmb-input {

  position: relative;

  padding-bottom: 0; }

  .calendar .rwmb-input input, .calendar .rwmb-input select, .adults .rwmb-input input, .adults .rwmb-input select, .children .rwmb-input input, .children .rwmb-input select, .full-name .rwmb-input input, .full-name .rwmb-input select, .email .rwmb-input input, .email .rwmb-input select, .phone .rwmb-input input, .phone .rwmb-input select, .book .rwmb-input input, .book .rwmb-input select, .black-tie .rwmb-input input, .black-tie .rwmb-input select {

    padding-left: 40px; }

  .calendar .rwmb-input::before, .adults .rwmb-input::before, .children .rwmb-input::before, .full-name .rwmb-input::before, .email .rwmb-input::before, .phone .rwmb-input::before, .book .rwmb-input::before, .black-tie .rwmb-input::before {

    font-family: FontAwesome;

    font-style: normal;

    font-weight: normal;

    text-decoration: inherit;

    color: #000;

    font-size: 18px;

    padding-right: 0.5em;

    position: absolute;

    top: 0px;

    bottom: 0;

    left: 0;

    background: #f0f0f0;

    display: flex;

    align-items: center;

    justify-content: center;

    padding: 0 8px; }

.calendar .rwmb-input::before {

  content: "\f073"; }

.adults .rwmb-input::before, .children .rwmb-input::before, .full-name .rwmb-input::before {

  content: "\f007"; }

.email .rwmb-input::before {

  content: "\f003"; }

.phone .rwmb-input::before {

  content: "\f095"; }

.book .rwmb-input::before {

  content: "\f02d"; }

.black-tie .rwmb-input::before {

  content: "\f27e"; }

.col-left, .col-right {

  background: #efefef; }

.col-left {

  padding: 10px; }

.col-right .eci {

  padding: 10px;

  margin-top: 0;

}</pre>

In there, <code>reservation-page</code> is the name of the template file. And, for styling the fields, I set classes for those fields in their advanced settings and use those classes in this code such as <code>calendar</code>, <code>adults</code>, <code>phone</code>, <code>book</code>, etc.

I'm using FontAwesome here, so I must enqueue it in the <code>functions.php</code> file. If you add the above CSS to the <code>style.css</code> file as I did, you also need to enqueue it as well.

<pre>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');

    wp_enqueue_style('estar-child-style', get_stylesheet_directory_uri() . '/style.css', false, '1.0', 'all');

}

add_action( 'wp_enqueue_scripts', 'child_theme_scripts' );</pre>

And now, the page turns to this look:

<img class="aligncenter" src="https://i.imgur.com/4AhL9bF.png" alt="the reservation page after styling by CSS" width="1920" height="942" />

<h2>Step 4: Add Functions to Figure Out the Amount</h2>

The Amount field is $0 so far no matter what you change the information in the fields many times. To figure out the amount then apply the number into this field, we must use code.

<h3>Create a Formula for the Amount</h3>

In the <code>functions.php</code> file, I'm adding this code:

<pre>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();

}</pre>

<span style="text-decoration: underline;"><strong>Explanation:</strong></span>

<ul>

   <li><code>add_action ()</code>: is a hook to create an action named <code>total</code>;</li>

   <li><code>text_domain_load_total_ajax()</code>: is the function to load the values of all the variables and then put them into the formula;</li>

   <li><code>$adults</code>, <code>$children</code>, <code>$set_lunch</code>, <code>$private_room</code>: are the variables;</li>

   <li><code>$adults = $_POST['adults'] ?? 1</code>: is to check if the <code>adults</code> 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;</li>

   <li><code>wp_send_json_success( $total )</code>: is to return the value of the <code>total</code> variable in json.</li>

</ul>

<h3>Load the Values of the Variables</h3>

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 option 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 follow:

<pre>(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 occured: ' + textStatus, errorThrown );

                }

            } )

        } )

    })

})(jQuery)</pre>

<img class="aligncenter" src="https://i.imgur.com/xDLdHTy.png" alt="create a JS file to load the value and send it to the function" width="1920" height="976" />

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

<ul>

   <li><code>#adults</code>, <code>#children</code>, <code>#set_lunch</code>, <code>#private_room</code>: are the IDs of the custom fields in the reservation form;</li>

   <li><code>adults</code>, <code>children</code>, <code>set_lunch</code>, <code>private_room</code>: are the variables using for the above formula, I'm assigning the values of the fields with the corresponding IDs to them;</li>

   <li><code>action: 'total'</code>: The values are being sent by ajax to this action with the url is <code>url : ReservationParams.ajaxURL</code>;</li>

   <li><code>success: function(response)</code>: 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 <code>amount</code>.</li>

</ul>

<h3>Enqueue the JS file and URL of AJAX</h3>

The formula hasn't been run yet because we haven't enqueued either the <code>script.js</code> file and the <code>url</code> of ajax. So now, add this code to the <code>functions.php</code>, inside the <code>child_theme_scripts()</code> function we have added before.

<pre>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' =&gt; admin_url( 'admin-ajax.php' ) ) );</pre>

<img class="aligncenter" src="https://i.imgur.com/L3iI1Qf.png" alt="enqueue the JS file and URL of the AJAX in the functions file to enable them" width="1920" height="942" />

<span style="text-decoration: underline;"><strong>Note:</strong></span>

<ul>

   <li><code>'theme-script'</code>: this name is the one you created, please pay attention that you must set it be the same in both lines;</li>

   <li><code>'ReservationParams'</code> and <code>'ajaxURL'</code> are from <code>url : ReservationParams'.ajaxURL</code>, you can change them, but do it at both.</li>

</ul>

Finally, everything is done. Let's have a test.

<img class="aligncenter" src="https://i.imgur.com/uRsRHLS.gif" alt="check the reservation form operation and see it run well" width="1920" height="940" />

You also can see that my first reservation is in posts management as well.

<img class="aligncenter" src="https://i.imgur.com/18EEZSk.png" alt="my test reservation show up in the admin screen will full information" width="1920" height="942" />

<h2>Last Words</h2>

In fact, I have met just some cases that allow customers to order the food set and let them know the total amount in advance. I'm not sure that it is popular out there. However, it still is a good reference of operations in a submission form. I guess that you should have some variations when applying this tutorial, good luck!