Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
MT | ET | Force users to select at least one ticket or RSVP before submitting the form
<?php
/*
* Prevents users from adding tickets to cart unless they have selected at least one ticket
*/
function tribe_prevent_add_cart_empty() {
echo '
<script type="text/javascript">
jQuery( document ).ready(function(){
jQuery( "form.cart" ).each(function(){
var tribe_empty_form_message = "Please select at least one item before submitting";
var tribe_cart_submit_button = jQuery( this ).find( "button[type=submit]" )[0];
var tribe_checkout_fields = jQuery( this ).find( "input[type=number]" );
tribe_cart_submit_button.setCustomValidity( tribe_empty_form_message );
tribe_checkout_fields.each(function(){
jQuery( this ).on( "change", function(){
for (var i = 0, total_tickets = 0; i < tribe_checkout_fields.length; i++)
total_tickets = total_tickets + parseInt(tribe_checkout_fields[i].value);
if( total_tickets > 0 )
tribe_cart_submit_button.setCustomValidity( "" );
else
tribe_cart_submit_button.setCustomValidity( tribe_empty_form_message );
});
});
});
});
</script>';
}
add_action( 'tribe_events_single_event_after_the_meta', 'tribe_prevent_add_cart_empty' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment