Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active February 15, 2022 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cliffordp/c012f2945a22867a3af2c2878e06fffd to your computer and use it in GitHub Desktop.
Save cliffordp/c012f2945a22867a3af2c2878e06fffd to your computer and use it in GitHub Desktop.
Event Tickets Plus v4.6+ - 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 v4.6+ - 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 int Post ID of post the ticket is tied to
* @param Tribe__Tickets__Ticket_Object Ticket that was just saved
* @param array Ticket data
* @param string Commerce engine class
*/
add_action( 'event_tickets_after_save_ticket', 'cliff_one_wooticket_visible_per_event', 10, 4 );
function cliff_one_wooticket_visible_per_event( $post_id_attached_to, $ticket, $ticket_raw_data, $commerce_engine_class ) {
if ( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' !== $commerce_engine_class ) {
return;
}
// Get all this event's tickets
$event_all_tickets = Tribe__Tickets__Tickets::get_all_event_tickets( $post_id_attached_to );
// Ignore everything that's not a WooTicket
$event_all_wootickets = array();
foreach ( $event_all_tickets as $each_ticket ) {
if ( 'Tribe__Tickets_Plus__Commerce__WooCommerce__Main' == $each_ticket->provider_class ) {
$event_all_wootickets[] = $each_ticket;
}
}
// Get the least-expensive, in-stock WooTicket from all this event's WooTickets
$event_wootickets_in_stock = array();
foreach ( $event_all_wootickets as $each_ticket ) {
if ( $each_ticket->is_in_stock() ) {
$event_wootickets_in_stock[] = $each_ticket;
break;
}
}
// cannot use wc_list_pluck() because it skips "0" values
$available_ticket_prices = array();
foreach ( $event_all_wootickets as $each_ticket ) {
$available_ticket_prices[] = (int) $each_ticket->price;
}
$least_expensive_ticket_price = min( $available_ticket_prices );
$least_expensive_wooticket = '';
foreach ( $event_all_wootickets as $each_ticket ) {
if ( $least_expensive_ticket_price === (int) $each_ticket->price ) {
$least_expensive_wooticket = $each_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).
*
* @link https://gist.github.com/cliffordp/c012f2945a22867a3af2c2878e06fffd
*
* @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