Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active March 12, 2024 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrasguseo/4f9718cbd0d9c398d8b9755eee463d1d to your computer and use it in GitHub Desktop.
Save andrasguseo/4f9718cbd0d9c398d8b9755eee463d1d to your computer and use it in GitHub Desktop.
ET > Limit ticket quantity counter to 1
<?php
/**
* Limit ticket quantity counter to 1.
*
* Usage: Add the snippet to your functions.php file or with a plugin like Code Snippets.
*
* @author: Andras Guseo
*
* Plugins required: Event Tickets
* Created: February 7, 2024
*/
function et_ticket_max_limit() {
if ( is_admin() ) {
return;
}
if ( get_post_type() != 'tribe_events' ) {
return;
}
?>
<script>
// Add here the post IDs of the tickets which you want to limit to 1.
let tickets = [ '251', '252', '258' ];
const ticketLimit = 1;
let selectorString = '';
tickets.forEach(function(item,index) {
tickets[index] = '#tribe-tickets__tickets-item-quantity-number--' + item;
});
// Add listener to the "Get tickets" button.
document.querySelector('#tribe-tickets__tickets-form #tribe-tickets__tickets-submit').addEventListener('click', function() { delaySetCounter(tickets); });
// Call function to adjust step to 2.
setCounter(tickets);
// Add delay for the modal
function delaySetCounter(tickets) {
// Need to wait for DOM to be generated
setTimeout(function() {setCounter(tickets); }, 200);
}
// Adjust step to 2.
function setCounter(tickets) {
console.log("max1");
var fields = document.querySelectorAll(tickets.join(', '));
fields.forEach(function (item, index) {
item.setAttribute('max', ticketLimit);
});
}
</script>
<?php
}
add_action( 'wp_footer', 'et_ticket_max_limit' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment