Last active
February 14, 2016 20:59
-
-
Save jbrinley/6848286 to your computer and use it in GitHub Desktop.
Filter Modern Tribe's Events Calendar to show ticket prices from the WooCommerce Tickets add-on. Add this to your theme's functions.php, or put it in an mu-plugin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function my_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; | |
foreach ( $ticket_ids as $ticket_id ) { | |
$ticket = $wootickets->get_ticket($postId, $ticket_id); | |
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 ( $sold_out ) { // all of the tickets are sold out | |
return __('Sold Out'); | |
} | |
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 ) { | |
return $currency . $max_price; | |
} | |
return $currency . $min_price . ' - ' . $currency . $max_price; | |
} | |
return $cost; // return the default, if nothing above returned | |
} | |
add_filter( 'tribe_get_cost', 'my_wootickets_tribe_get_cost', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This doesn't seem to be working with TEC v4.0.x - either standard $cost is returned (as per line 41) or adding global $cost causes the condition on line 27 to be true resulting in 'Sold Out' being returned. I think the problem may be with the foreach loop not working after certain functions/classes have been deprecated in TEC v4+ & I haven't yet discovered what else needs to change other than
'TribeWooTickets'
being deprecated in favour of'Tribe__Events__Tickets__Woo__Main'
I also tried adding the 'out of stock' condition to include unlimited stock tickets, but so far no joy:
if ( $ticket->stock || Tribe__Tickets__Ticket_Object::UNLIMITED_STOCK === $ticket->stock() )
Will let you know if I discover more...