Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Event Tickets Plus - WooCommerce Tickets: Keep all but the lowest-priced WooTicket for each event as Hidden visibility. Change the least-expensive to Visible (Shop and search results). Filter Visible tickets to have a post title of "Tickets for {Event_Name}" while in Shop or Search.
<?php
/*
* Event Tickets Plus - WooCommerce Tickets: Keep all but the lowest-priced
* WooTicket for each event as Hidden visibility. Change the least-expensive to
* Visible (Shop and search results).
*
* This snippet only works with WooCommerce 3.0.0+.
* We could do the same logic on a different action, like when a ticket is
* deleted or an attendee is created / ticket is sold, but this probably isn't
* necessary. If the Shop displays $5 ticket and they go to the single event
* page and see $5 tickets are sold out, they could still buy a $10 ticket (a
* bit of bait and switch). Plus, once a new WooTicket is added or an existing
* WooTicket is created), it'll update to be the next-lowest-priced ticket that
* is still in-stock. As with all snippets, customize to your own needs...
*
* @link https://gist.github.com/cliffordp/c012f2945a22867a3af2c2878e06fffd
*
* @see cliff_filter_wooticket_titles_in_shop_and_search
*
* @param $ticket_id
* @param $event_id
*/
add_action( 'wootickets_after_save_ticket', 'cliff_one_wooticket_visible_per_event', 10, 2 );
function cliff_one_wooticket_visible_per_event( $ticket_id, $event_id ) {
// Get all this event's tickets
$event_all_tickets = Tribe__Tickets__Tickets::get_all_event_tickets( $event_id );
// Ignore everything that's not a WooTicket
$event_all_wootickets = array();
foreach ( $event_all_tickets as $ticket ) {
if ( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' == $ticket->provider_class ) {
$event_all_wootickets[] = $ticket;
}
}
// Get the least-expensive, in-stock WooTicket from all this event's WooTickets
$event_wootickets_in_stock = array();
foreach ( $event_all_wootickets as $ticket ) {
if ( $ticket->is_in_stock() ) {
$event_wootickets_in_stock[] = $ticket;
break;
}
}
// cannot use wc_list_pluck() because it skips "0" values
$available_ticket_prices = array();
foreach ( $event_all_wootickets as $ticket ) {
$available_ticket_prices[] = (int) $ticket->price;
}
$least_expensive_ticket_price = min( $available_ticket_prices );
$least_expensive_wooticket = '';
foreach ( $event_all_wootickets as $ticket ) {
if ( $least_expensive_ticket_price === (int) $ticket->price ) {
$least_expensive_wooticket = $ticket;
break;
}
}
// and set it to Visible
if ( is_object( $least_expensive_wooticket ) ) {
$product = wc_get_product( $least_expensive_wooticket->ID );
$product->set_catalog_visibility( 'visible' );
$product->save();
}
// then set all other WooTickets for this event (in-stock or not) to Hidden
foreach ( $event_all_wootickets as $wooticket ) {
if ( $least_expensive_wooticket->ID !== $wooticket->ID ) {
$product = wc_get_product( $wooticket->ID );
$product->set_catalog_visibility( 'hidden' );
$product->save();
}
}
}
/**
* Event Tickets Plus - WooCommerce Tickets: Filter Visible tickets to have a
* post title of "Tickets for {Event_Name}" while in Shop or Search.
*
* We filter the ticket name in Shop and Search so that an event with 2 tickets
* (e.g. Lower-Level and Balcony) but only 1 Visible (e.g. Balcony) does not
* appear simply as "Balcony" in the Shop and then customers are looking around
* within the Shop for a different type of ticket for the event (e.g. Lower-
* Level).
*
* @see cliff_one_wooticket_visible_per_event
*
* @param $title
* @param $id
*
* @return string
*/
add_filter( 'the_title', 'cliff_filter_wooticket_titles_in_shop_and_search', 10, 2 );
function cliff_filter_wooticket_titles_in_shop_and_search( $title, $id ) {
if (
! is_admin()
&& function_exists( 'is_shop' )
&& function_exists( 'wc_get_product' )
&& (
is_shop()
|| is_search()
)
&& wc_get_product( $id )
&& class_exists( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' )
) {
// Search for the non-deprecated custom field.
$event_id = (int) get_post_meta( $id, Tribe__Tickets_Plus__Commerce__WooCommerce__Main::ATTENDEE_EVENT_KEY, true );
if ( ! tribe_is_event( $event_id ) ) {
// Search for the deprecated custom field, from Tribe__Tickets_Plus__Commerce__WooCommerce__Main::$event_key
$event_id = (int) get_post_meta( $id, '_tribe_wooticket_for_event', true );
}
if ( tribe_is_event( $event_id ) ) {
$title = sprintf( 'Tickets for Event: %s', esc_html( get_the_title( $event_id ) ) );
}
}
return $title;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment