cliffordp
5/31/2018 - 5:48 AM

Event Tickets Plus: WooCommerce Cart: Prepend each Ticket name to include the applicable Event's Post Title (and Event Date if attached to a

Event Tickets Plus: WooCommerce Cart: Prepend each Ticket name to include the applicable Event's Post Title (and Event Date if attached to a TEC event) so as to help distinguish them from Tickets for multiple Events or other non-Ticket Products in the Cart. Demo video: https://cl.ly/1l0m3q3Y0f0n

<?php

/*
 * Event Tickets Plus: WooCommerce Cart: Prepend each Ticket name to include
 * the applicable Event's Post Title (and Event Date if attached to a TEC event)
 * so as to help distinguish them from Tickets for multiple Events or other
 * non-Ticket Products in the Cart.
 *
 * Example: A Ticket called "Adult Ticket" for the "Day at the Zoo" event that
 * starts June 30, 2018, would appear as:
 *   Ticket for Event: Day at the Zoo on 2018-06-30: Adult Ticket
 *
 * @link https://gist.github.com/cliffordp/63abddea69b60f616c1aec1c6bdfc299 This snippet.
 * @link https://cl.ly/1l0m3q3Y0f0n A 4-minute demonstration video.
 * @link https://gist.github.com/cliffordp/5bffd372db4ebf14482574ce9bb2479c Another snippet you might be interested in.
 *
 * @param string $value          The Product name.
 * @param object $product_object The Product object, e.g. instance of WC_Product_Simple.
*/
add_filter( 'woocommerce_product_get_name', 'cliff_wootickets_prepend_event_title', 10, 2 );
function cliff_wootickets_prepend_event_title( $value, $product_object ) {
	if (
		! is_cart()
		|| ! function_exists( 'tribe_events_get_ticket_event' )
	) {
		return $value;
	}

	$post_id = $product_object->get_id();

	$event = tribe_events_get_ticket_event( $post_id );

	if ( empty( $event->ID ) ) {
		return $value;
	}

	$event_id = $event->ID;

	if (
		function_exists( 'tribe_is_event' )
		&& tribe_is_event( $event_id )
	) {
		$event_title = get_the_title( $event_id );
		$start_date  = tribe_get_start_date( $event_id, false, 'Y-m-d' );
		$value       = sprintf( '<strong>%s</strong> on %s: <strong>%s</strong>', $event_title, $start_date, $value );
	}

	$post_type = get_post_type( $event_id );

	$post_type_object = get_post_type_object( $post_type );

	$post_type_singular_label = $post_type_object->labels->singular_name;

	$value = sprintf( '%s for %s: %s',
		esc_html__( 'Ticket', 'event-tickets' ),
		$post_type_singular_label,
		$value
	);

	// Could wrap in esc_attr() for extra safety, but then remove all <strong> HTML, above.
	return $value;
}