cliffordp
10/28/2016 - 4:07 PM

TEC - Get raw '_EventCost' field without running through is_numeric(), and add currency symbol only if cost begins with a number (and entire

TEC - Get raw '_EventCost' field without running through is_numeric(), and add currency symbol only if cost begins with a number (and entire cost value is not zero -- but number with leading zero would work) * Does NOT have any effect for an event with Tickets (because then cost can vary, like $0-30)

<?php

/**
  * TEC - Get raw '_EventCost' field without running through is_numeric(), and add currency symbol only if cost begins with a number (and entire cost value is not zero -- but number with leading zero would work)
  * Does NOT have any effect for an event with Tickets from https://wordpress.org/plugins/event-tickets/ (because then cost can vary, like $0-30)
  *
  * From https://gist.github.com/cliffordp/b74ef779f9dbfff291bc2fa4dfebad1e
  */

add_filter( 'tribe_get_cost', 'cliff_raw_event_cost_field_with_currency', 10, 2 );

function cliff_raw_event_cost_field_with_currency( $cost, $post_id ) {
	if ( function_exists( 'tribe_events_has_tickets' ) && ! tribe_events_has_tickets( $post_id ) ) {
		$full_cost = tribe_get_event_meta( $post_id, '_EventCost', true ); // Only gets first _EventCost value if an event has multiple entries for _EventCost, which it shouldn't
		
		if ( ! empty( $full_cost ) ) { // zero is empty
			if ( preg_match( "/^[0-9]/", $full_cost ) ) {
				if ( method_exists( Tribe__Events__Main::instance(), 'maybe_set_currency_symbol_with_post' ) ) {
					$currency_symbol = Tribe__Events__Main::instance()->maybe_set_currency_symbol_with_post( $post_id );
				}
				
				$reverse_position = tribe_get_option( 'reverseCurrencyPosition', false );
				
				$full_cost = $reverse_position ? $full_cost . ' ' . $currency_symbol : $currency_symbol . $full_cost;
			} else {
				// leave $full_cost unchanged --> no currency symbol
			}
		} else {
			// nothing
		}
	} else {
		$full_cost = $cost;
	}

	return sanitize_text_field( $full_cost );
}