Reduce the number of available tickets by X

Est. Reading: 2 minutes
By: FDM Digital
Created: 12/13/2017
Category:
Difficulty: Intermediate

Reduce the number of available tickets by X

×Warning: This tutorial was created 2537 days ago. Some of the information may be out of date with more recent versions of Formidable. Please proceed with caution and always perform a backup before adding custom code.

This is a variation to the existing KB article Decrease an available count in another form.

I wanted to decrease a number in another form by using a field in my current form which can be variable and therefore not just by a fixed number as per the existing article.

Here's the set-up:

Form 1: Event Management Form - Admin Only

This form is used for adding new events and contains the following fields (although you can add any fields you like):

  • Event Name
  • Event Date
  • Number of places available
  • Event Description

Form 2: Event Booking Form

This form is your event booking form and uses a Dynamic Dropdown field to look at Form 1 > Event Date and display the dates available.

It also uses a 2nd Dynamic List field to display the number of places still available and a hidden field to capture this number which can then be used elsewhere in the form to control conditional logic for your form elements if the number of places gets below X.

To make the number of seats decrease by a variable amount (instead of a fixed amount like the KB article shows) use this function:

add_action('frm_after_create_entry', 'after_entry_created', 30, 2);
function after_entry_created($entry_id, $form_id){
  if($form_id == 9){ //change 9 to the ID of your reservations form
    global $wpdb;
    $reward_ids = $_POST['item_meta'][95]; //change 95 to the ID of your Dynamic dropdown field in your reservations form
$booking_num = $_POST['item_meta'][96]; //change 96 to the ID of your booking places field in your reservations form
    $seat_count_field = 91; //change 91 to the ID of the available seats field in the event form
    foreach ( (array) $reward_ids as $reward_id ) {
        $available = FrmEntryMeta::get_entry_meta_by_field( $reward_id, $seat_count_field, $booking_num, true );
        $wpdb->update( $wpdb->prefix .'frm_item_metas', array( 'meta_value' => ( (int) $available-$booking_num ) ), array( 'item_id' => $reward_id, 'field_id' => $seat_count_field ) );

    }
  }
}

 

Field 95 is the ID of the Dynamic Dropdown field in your booking form.

Field 96 is the ID of the number of places that are being booked.

Field 91 is the ID of the number of places in Form 1

9 is the ID of Form 2

 

Enjoy 🙂

 

 

Leave a Reply

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crosschevron-leftchevron-rightarrow-right