Skip to content

Instantly share code, notes, and snippets.

@jtcote
Last active February 14, 2016 22:06
Show Gist options
  • Save jtcote/68313654cea6fb46f5f5 to your computer and use it in GitHub Desktop.
Save jtcote/68313654cea6fb46f5f5 to your computer and use it in GitHub Desktop.
Tribe Events Plugin - wootickets add-on, show ticket cost and availability
add_filter( 'tribe_get_cost', 'gum_wootickets_tribe_get_cost', 10, 3 );
function gum_wootickets_tribe_get_cost( $cost, $postId, $withCurrencySymbol ) {
if ( empty($cost) && class_exists('TribeWooTickets') ) {
// see if the event has tickets associated with it
$wootickets = TribeWooTickets::get_instance();
$ticket_ids = $wootickets->get_Tickets_ids( $postId );
if ( empty($ticket_ids) ) {
return '';
}
// see if any tickets remain, and what price range they have
$max_price = 0;
$min_price = 0;
$sold_out = TRUE;
$stock = 0;
$stock_msg = '';
foreach ( $ticket_ids as $ticket_id ) {
$ticket = $wootickets->get_ticket($postId, $ticket_id);
$stock = $ticket->stock;
if ( $ticket->stock ) {
$sold_out = FALSE;
$price = $ticket->price;
if ( $price > $max_price ) {
$max_price = $price;
}
if ( empty($min_price) || $price < $min_price ) {
$min_price = $price;
}
}
}
if ( $stock && $stock <= 5) {
$stock_msg = ' | <div class="stock-message-alert">Hurry, Only '. $stock .' Available</div>';
}
if ( $sold_out ) { // all of the tickets are sold out
return __(' <div class="stock-message-alert">Sold Out</div>');
}
if ( empty($max_price) ) { // none of the tickets costs anything
return __('Free');
}
// make a string showing the price (or range, if applicable)
$currency = tribe_get_option( 'defaultCurrencySymbol', '$' );
if ( empty($min_price) || $min_price == $max_price ) {
if ( tribe_is_view() || 'tribe_events' == get_post_type() || is_singular( 'tribe_events' )) {
if ( $stock && $stock < 5 && is_front_page()) {
return $currency .' '. $max_price . ' | <div class="stock-message-alert small">Only '. $stock .' Available</div>';
}
if (is_singular( 'tribe_events' )) {
return $currency .' '. $max_price . $stock_msg;
} else {
return ' '. $max_price . $stock_msg;
}
} else {
return ' '. $max_price;
}
}
return $min_price . ' - ' . $max_price;
}
return $cost; // return the default, if nothing above returned
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment