Skip to content

Instantly share code, notes, and snippets.

@GeoffEW
Created February 22, 2017 22:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GeoffEW/075787fd6b427e1c47be0d756ca5b26c to your computer and use it in GitHub Desktop.
Save GeoffEW/075787fd6b427e1c47be0d756ca5b26c to your computer and use it in GitHub Desktop.
<?php
/* Tribe, limit ticket qty */
function tribe_limit_tickets() {
?>
<script type="text/javascript">
jQuery(document).ready( function( $ ) {
// do this if tickets available
if ( $('.tribe-events-tickets').length ) {
// set max qty to 1
$('.tribe-events-tickets .tribe-ticket-quantity').attr('max', 1);
// run on input change
$('.tribe-events-tickets .tribe-ticket-quantity').change ( function ( ) {
// don't run the manually triggered change event
if ( $(this).val() == 0 ) return;
// make sure it's not more than 1
if ( $(this).val() > 1 ) $(this).val(1);
// change all inputs but this to 0
// manually trigger the change event so available stock gets updated
$('.tribe-events-tickets .tribe-ticket-quantity').not( $(this) ).val(0).change();
});
// add a oninput event
$('.tribe-events-tickets .tribe-ticket-quantity').on('input', function (e) {
$(this).change();
});
}
});
</script>
<?php
}
add_action('wp_head', 'tribe_limit_tickets');
@EddieLongStockings
Copy link

Updated for version for Events Tickets 4.11.1

<?php 
/* Tribe, limit ticket qty */
function my_footer_scripts(){
  ?>
    <script type="text/javascript">
        jQuery(document).ready( function( $ ) {
            console.log("PSM Plugin/MT Tweak Begun");
            // do this if tickets available
            if ( $('.tribe-events-tickets').length ) {
                // set max qty to 3
                $('.tribe-events-tickets .tribe-tickets-quantity').attr('max', 1);
                // run on input change
                $('.tribe-events-tickets .tribe-tickets-quantity').change ( function ( ) {
                    // don't run the manually triggered change event
                    if ( $(this).val() == 0 ) return;
                    // make sure it's not more than 1
                    if ( $(this).val() > 3 ) $(this).val(3);
                    // change all inputs but this to 0
                    // manually trigger the change event so available stock gets updated
                    $('.tribe-events-tickets .tribe-tickets-quantity').not( $(this) ).val(0).change();
                });
                // add a oninput event
                $('.tribe-events-tickets .tribe-tickets-quantity').on('input', function (e) {
                    $(this).change();
                });
            }
        });
    </script>
  <?php
}
add_action( 'wp_footer', 'my_footer_scripts' );

@pattyok
Copy link

pattyok commented Feb 28, 2020

You will want to set the max attribute to the same number in both places.

<?php 
/* Tribe, limit ticket qty */
function my_footer_scripts(){
  ?>
    <script type="text/javascript">
        jQuery(document).ready( function( $ ) {
            console.log("PSM Plugin/MT Tweak Begun");
            // do this if tickets available
            if ( $('.tribe-events-tickets').length ) {
                // set max qty to 3
                var maxQuantity = 3;
                $('.tribe-events-tickets .tribe-tickets-quantity').attr('max', maxQuantity);
                // run on input change
                $('.tribe-events-tickets .tribe-tickets-quantity').change ( function ( ) {
                    // don't run the manually triggered change event
                    if ( $(this).val() == 0 ) return;
                    // make sure it's not more than 1
                    if ( $(this).val() > maxQuantity ) $(this).val(maxQuantity);
                    // change all inputs but this to 0
                    // manually trigger the change event so available stock gets updated
                    $('.tribe-events-tickets .tribe-tickets-quantity').not( $(this) ).val(0).change();
                });
                // add a oninput event
                $('.tribe-events-tickets .tribe-tickets-quantity').on('input', function (e) {
                    $(this).change();
                });
            }
        });
    </script>
  <?php
}
add_action( 'wp_footer', 'my_footer_scripts' );

@southsideslim03
Copy link

If I wanted to set the minimum quantity to 2 based on a specific ticket type, which code would I use? Would I do something as follows:

`
<script type="text/javascript">
jQuery(document).ready( function( $ ) {
console.log("PSM Plugin/MT Tweak Begun");
// do this if tickets available
if ( $('.tribe-events-tickets').length ) {
if ($('.tribe-events-tickets').type = "Per Couple"){
// set min qty to 2
var minQuantity = 2;
$('.tribe-events-tickets .tribe-tickets-quantity').attr('max', minQuantity);
// run on input change
$('.tribe-events-tickets .tribe-tickets-quantity').change ( function ( ) {
// don't run the manually triggered change event
if ( $(this).val() == 0 ) return;
// make sure it's not more than 1
if ( $(this).val() &gt; maxQuantity ) $(this).val(minQuantity);
// change all inputs but this to 0
// manually trigger the change event so available stock gets updated
$('.tribe-events-tickets .tribe-tickets-quantity').not( $(this) ).val(0).change();
}

});
// add a oninput event
$('.tribe-events-tickets .tribe-tickets-quantity').on('input', function (e) {
$(this).change();
});
}
});
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment