• Resolved ThomasLG

    (@thomaslg)


    Hi!
    I’m trying to add new form fields using hooks/filters. So far I managed to add the field itself, the problem is storing and using the field input. I have been looking at the plugin functions, but don’t really get how I should use $this/$booking.
    It would be awesome if you could take a quick look at it and point me in the right direction. I’ll get straight to it, here’s the code so far (just using functions.php at the moment):

    // ADDING NEW FIELDS: WORKING!
    add_filter( 'rtb_booking_form_fields', 'buoy_custom_fields', 10, 2 );
    function buoy_custom_fields( $fields, $request ) {
      $fields['reservation']['fields']['retter']['title'] = 'Retter';
      $fields['reservation']['fields']['retter']['request_input'] = empty( $request->request_retter ) ? '' : $request->request_retter;
      $fields['reservation']['fields']['retter']['callback'] = 'rtb_print_form_text_field';
    
      return $fields;
    }
    
    // SAVING VALIDATED DATA: NOT WORKING!
    add_filter( 'rtb_validate_booking_submission', 'buoy_custom_load', 10, 3 );
    function buoy_custom_load( $this, $booking ) {
      $this->retter = empty( $_POST['rtb-retter'] ) ? '' : sanitize_text_field( stripslashes_deep( $_POST['rtb-retter'] ) );
    
    // I believe I should be adding something to $booking here?
    
      return $this;
    }
    
    // SAVING INPUT TO POST METADATA: NOT WORKING
    add_filter( 'rtb_insert_booking_metadata', 'buoy_insert_meta', 10, 2 );
    function buoy_insert_meta( $meta, $this) {
      $meta['retter'] = $_POST['rtb-retter'] ;
    // Tried using "$this->booking->retter" etc. - produces error msg
    
      return $meta;
    }
    
    // DISPLAY INPUT IN MAIL BY TEMPLATE TAG: TAG IS DISPLAYED, NO INPUT DATA
    add_filter( 'process_template', 'buoy_template_tags', 10, 2 );
    function buoy_template_tags( $template_tags, $notification) {
      $template_tags['{retter}'] = "test";
    
      return $template_tags;
    }

    https://www.ads-software.com/plugins/restaurant-reservations/

Viewing 2 replies - 16 through 17 (of 17 total)
  • Hi Thomas,

    Thanks for the kind words. If you get a chance, I always appreciate reviews for the plugin.

    I think it would be possible to submit the front-end form via Ajax, but it wouldn’t be trivial. The next version of the plugin allows you to add/edit bookings from the backend with an Ajax form. The technique I used there was the following:

    1. Print a custom version of the form fields without the regular button. Print a custom button that fires off an Ajax request with the form fields.

    2. Receive the Ajax request on the server and set up the POST variables as expected. Then run the request through the booking validation.

    3. If the validation fails, generate an entirely new set of the booking form fields with the error messages. Pass this HTML blob back in a response to the Ajax request.

    4. Receive the response and replace the field with the new HTML blob.

    As I said, it’s not a trivial problem. But if you want to take a stab at it, you’ll want to take a look at how I fire off and handle the Ajax request and how that request is handled on the server.

Viewing 2 replies - 16 through 17 (of 17 total)
  • The topic ‘New fields using hooks’ is closed to new replies.