How to conditionally make booking forms hidden

WordPress Event Management, Calendars & Registration Forums General Question How to conditionally make booking forms hidden

This topic contains 5 replies, has 2 voices, and was last updated by  Stephen Harris 9 years, 1 month ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #21381

    Hello,

    I need to make some booking forms accessible to members of my site and some accessible to the general public.

    How do you recommend I add conditional logic to hide the form if the event is “members only”?

    Thanks for any advice.

    CYCOffice
    #21382

    Hi Louise,

    You’ll want to make two changes. One to the booking form template (templates/eo-booking-form.php) to hide the booking form. There is already logic in that template relating to whether a form should be displayed, so you can adapt it there. How to check whether the user is actually a member will depend on the membership plug-in you are using.

    Secondly, as a ‘security’ measure, you’ll want to check when the booking form is being processed that the current user is not a member. While the booking form wouldn’t have been displayed a user could still making a booking with an appropriately crafted POST request.

    To prevent this, just add the following code, adapted for your membership plug-in.

     add_action( 'eventorganiser_validate_booking_form_submission', function( $form ) {
         if ( /* user is not a member */ ) {
               $form->add_error( 'members-only', 'You must be a member to place a booking' );
         }
     } );
    

    I would recommend implementing this first, so that you can easily test it before hiding the booking form.

    Stephen Harris
    #21387

    Thank you Stephen,

    I can now see how I can hide the form.

    How do you recommend I configure “member only” or “General Public” on an event by event basis. This needs to be something that a person creating a new event can set.

    For example, is there a way to add a checkbox to the event setup box to select it as a “members only” event?

    CYCOffice
    #21403

    Hi Louise,

    You can get the event ID from the form as follows:

     $event_id = $form->get( 'event_id' );
    

    The event ID is the post ID (an event is of ‘event’ type). So you can do one of two things:

    1. Use a custom taxonomy (or even event tags / categories) as a way of marking an event as members only or not.
    2. Use a custom field (optionally you can create your own metabox).

    Both 1 and 2 can be done with the standard WordPress API.

    Stephen Harris
    #21422

    Thanks again,

    I tried editing a child template version of eo-booking-form.php to add a test for if the Member Only category was set. but it never goes to the echo statement below.
    I must be doing something wrong, sorry, but still feeling my way with php.

            if ( !is_user_logged_in() && in_category('Member Only') ) {
                echo "Member only event - login to make a booking";
            }
            else { //logged in or "public event"
                //Display the booking form
                ?>
                <form method="post" action="<?php echo get_permalink().'#eo-bookings';?>" id="eo-booking-form" autocomplete="off">
    
                    <?php 
                        //Display custom fields
                        $this->display_form_fields();
                    ?>
    
                </form>
    
    CYCOffice
    #21426

    in_category() is for posts not events. You want:

     is_object_in_term( get_the_ID(), 'event-category', 'Member Only' );
    
    Stephen Harris
Viewing 6 posts - 1 through 6 (of 6 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.