Created
February 27, 2018 17:54
-
-
Save jentheo/a4bb757ea428fc2eec42a83204571c61 to your computer and use it in GitHub Desktop.
Prevents users from adding tickets to cart unless they have selected at least one ticket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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() { | |
this.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( "" ); | |
tribe_checkout_fields.each(function() { | |
this.setCustomValidity( "" ); | |
}); | |
else | |
//tribe_cart_submit_button.setCustomValidity( tribe_empty_form_message ); | |
tribe_checkout_fields.each(function() { | |
this.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