bookee_can_cancel in eo_events

This topic contains 4 replies, has 2 voices, and was last updated by  Benjamin Ogg 8 years, 1 month ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #25117

    Hi Stephen,
    with every hour i work with your plugin I like it more and more! Thanks for your awesome work.
    Is there any way that a bookee (user) can cancel an event in the eo_events overview.
    In other words: I would like to list all events of a bookee, ordered by event-date where the bookee has a «cancel»-link behind every event. This would be highly helpful.
    Thank you and best regards,
    Benjamin.

    Benjamin Ogg
    #25187

    Hi Benjamin,

    Glad you like the plug-in! There is the [booking_history] shortcode with which you can allow bookee’s to cancel a booking:

    [booking_history bookee_can_cancel=true]
    
    Stephen Harris
    #25193

    Hi Stephen

    Thanks for your answer. I did know about the [booking_history] shortcode. But as far as I know list can not be sorted by event date, which is in my case, a must.
    Or is it possible to have the list sorted by event date?

    Cheers, Benjamin

    Benjamin Ogg
    #25310

    That’s true, it only orders by booking date currently.

    I’m afraid you’d need to role your own solution here.

    One (minor) complication is that you can have multiple bookings for one event. So you may want to decide how you handle that. Cancel all bookings? Or have a cancel link for each booking.

    Below is an example of how to handle the latter. If doing the former, you would pass an event ID (and occurrence ID if booking by date) and find the user’s bookings for that event (or occurrence) and cancel them all.

    Simply create a link of the form:

    yoursite.com?booking_id=<booking-id>&booking_action=cancel&nonce=<nonce>
    

    Then use template_redirect to listen such a url being hit. Then validate the user, and perform the cancellation. You’ll then need to decide where to redirect the user (i.e. redirect them to a confirmation page).

    You could do something a bit more complicated by using session tokens and listen the booking(s) they’ve just cancelled, but that’s a bit beyond the scope here.

    add_action( 'template_redirect', function() {
         //Check the url is the one we're targetting
         if ( empty( $_GET['booking_id'] ) || empty( $_GET['nonce'] ) || empty( $_GET['booking_action'] ) ) {
              return;
         }
         if ( 'cancel' !== $_GET['booking_action'] ) {
             return;
         }
    
         //Check the user is logged-in
         if ( ! user_logged_in() ) {
             wp_die( 'You must log-in to cancel bookings' );
         }
    
         $booking_id = (int) $_GET['booking_id'];
         $nonce =  $_GET['nonce'];
    
         //Check user is authorised to cancel this booking (i.e. is the bookee);
         if ( get_current_user_id() !== eo_get_booking_meta( $booking_id, 'bookee' ) ) {
             wp_die( 'You do not have permission to cancel this booking' );
         }
    
         //Check the nonce
         if( ! wp_verify_nonce( $nonce, 'cancel_booking' ) ) {
             wp_die( 'You are not authorised to cancel this booking' );
         }
    
        //Finally cancel the booking and redirect the user.
        eo_cancel_booking( $booking_id );
    
         wp_redirect( 'yoursite.com/redirect/to/page' );
         exit;
    } );
    

    It’s very much a ‘proof of concept’, but I hope it helps! You may also want to improve the way errors are handled.

    • This reply was modified 8 years, 1 month ago by  Stephen Harris.
    Stephen Harris
    #25365

    Thank you so much, dear Stephen! That helped me a lot and I got it set up like that.
    Cheers, Benjamin

    Benjamin Ogg
Viewing 5 posts - 1 through 5 (of 5 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.