Support

Home Forums Event Espresso Premium Adapting function to work with datetimes

Adapting function to work with datetimes

Posted: June 3, 2020 at 6:11 am


Nick

June 3, 2020 at 6:11 am

Hello,

I am using the function included below in my theme’s function.php file. It displays the number of places left for an event or that it is sold out.

Typically I only have one datetime for an event, and the function works fine, but I am now running a weekly group where people can sign up each week and so I have one event with different datetimes. Mostly I have everything set up as I want. I duplicate tickets in advance, associate them with the relevant datetime, and have them ready to go on sale when the current meeting has ended, so that people can book for the next week directly after the meeting.

The function below isn’t working as expected though. The first meeting has now passed and so I would like it to display:

“There are 8 spaces left for this event” (for the new/current datetime which is what shows on the page for the event)

But instead it shows:

“3 June 2020 is now sold out” (for the old event)

Can the function below be adapted so that the information for the current/next datetime is displayed?

Thanks,

Nick

// print the remaining tickets left for each datetime
add_action( 'AHEE__ticket_selector_chart__template__after_ticket_selector', 'ee_print_tickets_left_after_selector', 10, 2 );
function ee_print_tickets_left_after_selector( $EVT_ID, $event ) {
echo '<ul id="remaining">';
$datetimes = $event->datetimes();
foreach($datetimes as $datetime){
if ( $datetime instanceof EE_Datetime ) {
$dtt_name = $datetime->get('DTT_name');
$name = !empty($dtt_name) ? ($datetime->name()) : 'this event';
$limit = $datetime->get('DTT_reg_limit');
$tickets = $datetime->ticket_types_available_for_purchase();
$all_ticket_registrations = 0;
if( ! empty($tickets) ) {
foreach($tickets as $ticket) {
$all_ticket_registrations += $ticket->count_registrations( array( array( 'STS_ID' => array( 'IN', array( EEM_Registration::status_id_pending_payment, EEM_Registration::status_id_approved ) ) ) ) );
}
}
$remain = $limit - $all_ticket_registrations;
$remain = $limit == EE_INF ? 'a lot of' : $remain;
if ( $datetime->sold_out() ) {
echo ucfirst($name) . ' is now <b>sold out</b>.';
} else {
printf( _nx(
'%1$sThere is <b>one space</b> left for %3$s',
'%1$sThere are <b>%2$s spaces</b> left for %3$s',
$remain, 'event ticket info', 'event_espresso' ),
'<li style="list-style-type:none;">', $remain, '<span>' . $name . '</span>'
);
}
}
}
echo '';
// Adds link to next seminar when event sold
$event = EEH_Event_View::get_event();
if ( $event instanceof EE_Event ) {
if ( $event->is_sold_out() || $event->is_sold_out( TRUE ) ) {
echo '<p style="text-align:center">Details of our next seminars can be found here.';
}
}
}


Tony

  • Support Staff

June 4, 2020 at 4:43 am

Hi Nick,

Change $datetimes = $event->datetimes();

To $datetimes = $event->primary_datetime();

primary_datetime() will return the next upcoming DateTime until all datetimes are expired, once they are all expired the ‘first’ DateTime (order by start date/time) will be returned.

If you don’t want that to happen there are multiple methods in The EEM_Datetime model to pull specific datetimes from an event.

Also, a couple of notes for your function, you don’t need this line:

$event = EEH_Event_View::get_event();

The event is already passed into the filter.

This $event->is_sold_out( TRUE ) is forcing a fair amount of calculations to be processed each an every time the ticket selector is loaded, most of which will have been done when they needed to have been already so just use:

if ( $event->is_sold_out() ) {

is_sold_out() simply uses the status of the event to determine if the event is sold out.

is_sold_out($actual = true) pulls all tickets and calculates the spaces remaining on each and every ticket/DateTime within the event. For a simple output like the above, you don’t need that kind of accuracy 95% of the time and the status alone is fine. If you do find that section doesn’t show the sold out message when expected (I don’t think you will) you can switch back if needed.


Nick

June 12, 2020 at 5:12 am

Thanks very much Tony.

I replaced:

$datetimes = $event->datetimes();
foreach($datetimes as $datetime){...}

with

$datetime = $event->primary_datetime();

and all is working as I hoped. As I am only pulling in one datetime now, the next upcoming one, I didn’t need the ‘foreach’ line.

Thanks also for the other tips about the function. I appreciate that.


Tony

  • Support Staff

June 12, 2020 at 6:25 am

You’re most welcome, glad it’s working for you.

You must be logged in to reply to this support post. Sign In or Register for an Account

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3 Documentation for Event Espresso 4

Status: publish

Updated by  Tony 3 weeks, 6 days ago ago

Topic Tags

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!