Home › Forums › Ticket Products › Event Tickets Plus › Check whether an event has tickets or not
- This topic has 0 replies, 2 voices, and was last updated 8 years ago by
dannycare.
-
AuthorPosts
-
March 25, 2018 at 9:12 am #1488066
dannycare
ParticipantHi! I’m currently writing some custom stuff in the events single page and need some help with a particular problem.
The client required that a certain amount of tickets needed to be sold for an event to go ahead. If over 50% of the tickets were sold for an event we would display a message on the event page saying that it was guaranteed to go ahead.
I wrote this snippet of code that does just that. It goes in the single event page:
<?php
// Get Ticket ID by searching for Event ID
function get_tickets_for_event( $event_id ) {
$tickets = Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
$ticket_data = array();// Build a list of ticket names and prices
foreach ( $tickets as $single_ticket ) {
$product = wc_get_product( $single_ticket->ID );
$ticket_data[] = array(
$product->get_id()
);
}// Return Array
return $ticket_data;}
// We need to assign the foreach result as a variable so it can be called elsewhere.
foreach ( get_tickets_for_event( $event_id ) as $ticket ) {
$ticket_id .= $ticket[0].'';
}// Get Remaining Tickets and Total Capacity of Tickets for that event.
$remaining = tribe_events_count_available_tickets();
$capacity = tribe_tickets_get_capacity($ticket_id);// Calculate percentage based on remaining tickets and total capacity
$percentage = ($remaining * 100) / $capacity;?>
And then this goes within the loop:
<?php if ($percentage >= 1 && $percentage <= 50) : ?>This event is guaranteed to go ahead
<?php elseif($percentage > 50) : ?>
Currently taking bookings
<?php elseif($percentage == 0) : ?>
Fully booked
<?php endif; ?>
My problem is however, some of the events do not have tickets for sale, and when this happens it gives me an error on the page.
I need to be able to check whether the event shown has any tickets at all, and if it doesn’t, don’t display anything.
I’ve tried the following code wrapped around it and it does not work:
<?php if(tribe_events_has_tickets()) : ?>
Show custom messages (in code above)
<?php endif; ?>
Any suggestions here? Thanks.
March 26, 2018 at 7:26 am #1488551Victor
MemberHi Danny!
Thanks for reaching out to us!
First, please let me note that we are fairly limited in how much support we can give for custom development questions like that.
That said, we always like helping out and at least point users into the right direction as much possible.
The tribe_events_has_tickets() function should work the way you are using it. Could you please let me know how it’s not working for you?
I’m not exactly sure where you are placing that check. Could you please share with us a copy of the complete single-event.php template file? I’ll try it out and see if I can find why it’s not working.
Let me know.
Best,
VictorApril 17, 2018 at 9:35 am #1509387Support Droid
KeymasterHey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.
Thanks so much!
The Events Calendar Support Team -
AuthorPosts
- The topic ‘Check whether an event has tickets or not’ is closed to new replies.
