cliffordp
7/22/2016 - 8:56 PM

Event Tickets: Protect against sending Submit form multiple times

Event Tickets: Protect against sending Submit form multiple times

<?php

/**
 * Event Tickets / ET+: Disable the Ticket form's Submit button after initial
 * click (by adding 10-second delay) to prevent accidental multiple purchases.
 *
 * From https://gist.github.com/cliffordp/8bceb54b440fbe9de1d42df6f231cc7a This snippet by Matt B., AndLen, and Cliff.
 * Updated June 13, 2018.
 **/
function tribe_snippet_prevent_multi_click_on_submit() {
	if (
		! is_singular()
		|| ! function_exists( 'tribe_events_has_tickets_on_sale' )
		|| ! tribe_events_has_tickets_on_sale( get_the_ID() )
	) {
		return false;
	}

	wp_enqueue_script( 'jquery' );
	?>
    <script>
		jQuery( document ).ready( function ( $ ) {
			// RSVP
			var $et_rsvp_form = $( 'form#rsvp-now' );
			var $et_rsvp_button = $et_rsvp_form.find( 'button[name="tickets_process"]' );
			$et_rsvp_form.submit( function () {
				$et_rsvp_button.on( 'click', function () {
					return false;
				} );
				setTimeout( function () {
					$et_rsvp_button.off( 'click' );
				}, 10000 );
			} );

			// Tribe Commerce PayPal
			var $tribe_paypal_form = $( 'form#tpp-buy-tickets' );
			var $tribe_paypal_button = $tribe_paypal_form.find( 'button.tpp-submit' );
			$tribe_paypal_form.submit( function () {
				$tribe_paypal_button.on( 'click', function () {
					return false;
				} );
				setTimeout( function () {
					$tribe_paypal_button.off( 'click' );
				}, 10000 );
			} );

			// Event Tickets Plus (same for WooCommerce and EDD Tickets, assuming they aren't both displayed at the same time)
			var $et_plus_form = $( 'form#buy-tickets' );

			// WooCommerce
			var $et_plus_woo_button = $et_plus_form.find( 'button[name="wootickets_process"]' );
			$et_plus_form.submit( function () {
				$et_plus_woo_button.on( 'click', function () {
					return false;
				} );
				setTimeout( function () {
					$et_plus_woo_button.off( 'click' );
				}, 10000 );
			} );

			// EDD
			var $et_plus_edd_button = $et_plus_form.find( 'button.edd-submit' );
			$et_plus_form.submit( function () {
				$et_plus_edd_button.on( 'click', function () {
					return false;
				} );
				setTimeout( function () {
					$et_plus_edd_button.off( 'click' );
				}, 10000 );
			} );
		} );
    </script>
	<?php
}

add_action( 'wp_footer', 'tribe_snippet_prevent_multi_click_on_submit' );