{"id":1488066,"date":"2018-03-25T09:12:50","date_gmt":"2018-03-25T16:12:50","guid":{"rendered":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/"},"modified":"2018-03-25T09:12:50","modified_gmt":"2018-03-25T16:12:50","slug":"check-whether-an-event-has-tickets-or-not","status":"closed","type":"topic","link":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/","title":{"rendered":"Check whether an event has tickets or not"},"content":{"rendered":"<p>Hi! I&#8217;m currently writing some custom stuff in the events single page and need some help with a particular problem.<\/p>\n<p>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.<\/p>\n<p>I wrote this snippet of code that does just that. It goes in the single event page:<\/p>\n<p><code><br \/>\n&lt;?php<br \/>\n\t\/\/ Get Ticket ID by searching for Event ID<br \/>\n\tfunction get_tickets_for_event( $event_id ) {<br \/>\n\t$tickets = Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );<br \/>\n\t$ticket_data = array();<\/p>\n<p>\t\/\/ Build a list of ticket names and prices<br \/>\n\tforeach ( $tickets as $single_ticket ) {<br \/>\n\t\t$product = wc_get_product( $single_ticket-&gt;ID );<br \/>\n\t\t$ticket_data[] = array(<br \/>\n\t\t$product-&gt;get_id()<br \/>\n\t\t);<br \/>\n\t}<\/p>\n<p> \t\/\/ Return Array<br \/>\n\treturn $ticket_data;<\/p>\n<p>\t}<\/p>\n<p>\t\/\/ We need to assign the foreach result as a variable so it can be called elsewhere.<br \/>\n\tforeach ( get_tickets_for_event( $event_id ) as $ticket ) {<br \/>\n\t\t$ticket_id .= $ticket[0].'';<br \/>\n\t}<\/p>\n<p>\t\/\/ Get Remaining Tickets and Total Capacity of Tickets for that event.<br \/>\n\t$remaining = tribe_events_count_available_tickets();<br \/>\n\t$capacity = tribe_tickets_get_capacity($ticket_id);<\/p>\n<p>\t\/\/ Calculate percentage based on remaining tickets and total capacity<br \/>\n        $percentage = ($remaining * 100) \/ $capacity;<\/p>\n<p>?&gt;<br \/>\n<\/code><\/p>\n<p>And then this goes within the loop:<\/p>\n<p><code><br \/>\n&lt;?php if ($percentage &gt;= 1 &amp;&amp; $percentage &lt;= 50) : ?&gt;<\/p>\n<p>\tThis event is guaranteed to go ahead<\/p>\n<p>&lt;?php elseif($percentage &gt; 50) : ?&gt;<\/p>\n<p>\tCurrently taking bookings<\/p>\n<p>&lt;?php elseif($percentage == 0) : ?&gt;<\/p>\n<p>\tFully booked<\/p>\n<p>&lt;?php endif; ?&gt;<br \/>\n<\/code><\/p>\n<p>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.<\/p>\n<p>I need to be able to check whether the event shown has any tickets at all, and if it doesn&#8217;t, don&#8217;t display anything.<\/p>\n<p>I&#8217;ve tried the following code wrapped around it and it does not work:<\/p>\n<p><code><br \/>\n&lt;?php if(tribe_events_has_tickets()) : ?&gt;<br \/>\nShow custom messages (in code above)<br \/>\n&lt;?php endif; ?&gt;<br \/>\n<\/code><\/p>\n<p>Any suggestions here? Thanks.<\/p>\n","protected":false},"template":"","class_list":["post-1488066","topic","type-topic","status-closed","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Check whether an event has tickets or not -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Check whether an event has tickets or not -\" \/>\n<meta property=\"og:description\" content=\"Hi! I&#8217;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 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/\",\"url\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/\",\"name\":\"Check whether an event has tickets or not -\",\"isPartOf\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\"},\"datePublished\":\"2018-03-25T16:12:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/theeventscalendar.com\/support\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Topics\",\"item\":\"https:\/\/theeventscalendar.com\/support\/topics\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Ticket Products\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/event-tickets\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Event Tickets Plus\",\"item\":\"https:\/\/theeventscalendar.com\/support\/forums\/forum\/event-tickets\/event-tickets-plus\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"Check whether an event has tickets or not\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/theeventscalendar.com\/support\/#website\",\"url\":\"https:\/\/theeventscalendar.com\/support\/\",\"name\":\"\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Check whether an event has tickets or not -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/","og_locale":"en_US","og_type":"article","og_title":"Check whether an event has tickets or not -","og_description":"Hi! I&#8217;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 [&hellip;]","og_url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/","url":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/","name":"Check whether an event has tickets or not -","isPartOf":{"@id":"https:\/\/theeventscalendar.com\/support\/#website"},"datePublished":"2018-03-25T16:12:50+00:00","breadcrumb":{"@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/theeventscalendar.com\/support\/forums\/topic\/check-whether-an-event-has-tickets-or-not\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/theeventscalendar.com\/support\/"},{"@type":"ListItem","position":2,"name":"Topics","item":"https:\/\/theeventscalendar.com\/support\/topics\/"},{"@type":"ListItem","position":3,"name":"Ticket Products","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/event-tickets\/"},{"@type":"ListItem","position":4,"name":"Event Tickets Plus","item":"https:\/\/theeventscalendar.com\/support\/forums\/forum\/event-tickets\/event-tickets-plus\/"},{"@type":"ListItem","position":5,"name":"Check whether an event has tickets or not"}]},{"@type":"WebSite","@id":"https:\/\/theeventscalendar.com\/support\/#website","url":"https:\/\/theeventscalendar.com\/support\/","name":"","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/theeventscalendar.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1488066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/topic\/1488066\/revisions"}],"wp:attachment":[{"href":"https:\/\/theeventscalendar.com\/support\/wp-json\/wp\/v2\/media?parent=1488066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}