Support

Home Forums Event Espresso Premium How do I display the total number of tickets remaining for each event?

How do I display the total number of tickets remaining for each event?

Posted: February 12, 2024 at 2:53 pm


jcforrester

February 12, 2024 at 2:53 pm

I’m querying through all events via a while loop to display them as cards on the frontpage, which display the featured image, title, excerpt, etc.

I was wondering if it was possible to show how many total tickets are remaining for each event, adding up all the datetimes? So if my event has 2 times, with 10 tickets each, it would show: “tickets remaining: 20.”

Im probably in over my head here (so forgive me if this isn’t even close), but something like this:

<?php
echo ‘<p>Total spaces remaining: ‘ . get_post_meta(get_the_ID(), ‘????’, true) . ‘</p>’;
?>

If you need to see the full context of the while loop, I can absolutely do that.
Thanks in advance!


Rio

  • Support Staff

February 12, 2024 at 5:29 pm

Are you referring to this?
https://monosnap.com/file/ojQKm0VfCiMyI7Fsh70Q4MS9UNp2SC
then, just enable it by going
Event Espresso -> Events -> Templates

Set ‘Show Ticket Details?’ and ‘Show Ticket Sale Info?’ to yes and save.

An additional details section will be added to the ticket selector where the attendees can view the number of remaining tickets.

For adding custom function, there’s thread that might help you, check this.
https://eventespresso.com/topic/show-remaining-tickets-per-ticket-type/

thanks


jcforrester

February 13, 2024 at 9:38 am

Hey!

So no, that isn’t exactly what Im talking about. I mean, yes it kind of is, but the wrong location. Im trying to display that number on the page where all the events live. So if I have 10 events with 2 datetimes each, that page will have ALL the ticket amounts listed for each one.

So on (let’s just say) /events:
Event 1: 22 tickets remaining
Event 2: 53 tickets remaining
Event 3: 9 tickets remaining
… so on.

And those numbers are summing up all available datetimes. And then each one of those cards will have a link where you can register for the event.

Think of it like searching for something on Etsy, when you run a search, you see a grid with all the items (in my case events), and each item tells you how many are left of that item (in my case tickets), but you have to click on the item to actually buy it (in my case register).

TLDR: What you showed is what I want, I just want to show all of them on the same page when looping through all available events.

Does that make more sense? If not I’ll be happy to try to explain in more detail.

Thanks!


Rio

  • Support Staff

February 13, 2024 at 2:27 pm

We have available snippet to show it in event page https://monosnap.com/file/zKry9TffhetI9qOjJnKNY7YQRElPj4

You can check the snippet here.
https://gist.github.com/joshfeck/c7fb0043e2bbe9bb3619

There’s a thread with regards to that.
https://eventespresso.com/topic/show-tickets-left-counter-in-overview/

thanks.


Tony

  • Support Staff

February 13, 2024 at 2:30 pm

Hi there,

How are you pulling in the events?

Are you using the model system (See HERE)?

If not… I recommend you do so as it makes this easier.

Add your code to PasteBin and post the link here so we can take a look.

If you are using the model system what you need is either:

$event->spaces_remaining_for_sale();

Or:

$event->spaces_remaining();

Assuming $event is an instance of EE_Event.


jcforrester

February 14, 2024 at 2:58 pm

Hey Tony!
So. I didn’t know this existed.
Kinda wanna throw up now lol. If I wasn’t in over my head before I certainly am now.

So, as a win, this did solve the issue. I was able to switch over to the EE model and loop my posts, displaying the number of slots left in each one. If you want to mark the question resolved, you can, since my original question was solved.

I do have one more question about the model however. I’m happy to create a new post if you’d like, just let me know. 🙂

But in case I can just ask here… Im confused about how to display the thumbnail, URL to the event, and any custom ACF fields within the model. maybe Im supposed to use the model equivalent of “get_the_ID()”?

Code:

<?php
$events = EEM_Event::instance()->get_all();
foreach( $events as $event ) {
set_query_var( 'event', $event );
get_template_part('components/cards/_event-card');
} ?>

Displays all of the events using the event card template partial.

Inside of that card I have:
<?php echo $event->name(); ?>
<?php echo $event->permalink(); ?>

Top line works fine, displays what I’d expect. Bottom line throws an error.

Any suggestions on what code is needed to display a permalink?

Thanks in advance and I apologize again for being naive, PHP is still pretty new to me. You’ve been a huge help so far.

Thank you so much!


Tony

  • Support Staff

February 15, 2024 at 12:17 pm

So. I didn’t know this existed.
Kinda wanna throw up now lol. If I wasn’t in over my head before I certainly am now.

Haha! 🙂

So… the model system can look a little scarey at times but once you get used to it helps with pretty much anything within EE (there are times when you need to ‘roll your own code’ but the models help with at least 90% of use cases).

But in case I can just ask here… Im confused about how to display the thumbnail, URL to the event, and any custom ACF fields within the model. maybe Im supposed to use the model equivalent of “get_the_ID()”?

ACF fields are custom fields, which would be post_meta, or in this case event_meta.

We have a help function for post_meta:

$event->get_post_meta( $meta_key = null, $single = false);

BUT… your missing out on what ACF will do with its own custom fields if you use that. Use ACF’s own get_field():

https://www.advancedcustomfields.com/resources/get_field/

get_field($selector, [$post_id = false], [$format_value = true], [$escape_html = false]);

Pass it the Event ID.

get_field('my-key', $event->ID());

For the thumbnail we have another helper:

feature_image($size = 'thumbnail', $attr = '')

So $event->feature_image();

Any suggestions on what code is needed to display a permalink?

The best advice I can give you is to using something called Kint… the problem with that is its intended for development, someone added a plugin to use it within WP as you would any plugin…. but that’s no longer updated:

https://wordpress.org/plugins/kint-debugger/

I still use a version of that available here:

https://github.com/DuckDivers/kint-debugger

Click the Code button, then download zip.

Install that zip within Dashboard -> Plugins -> Add new -> Upload plugin.

Activate it.

Now… do this:

$event = EEM_Event::instance()->get_one_by_ID({event_id});
d($event);

Obviously, change {event_id} to be the ID of an event you want to work on.

Now you’ll get an much nicer output showing you al of the available values within EE_Event (or any object you pass it) and all of the available method on that object. Personally, I find kint really useful and if you want to know any more about it, start here:

https://letswp.justifiedgrid.com/kint-debugging-php-wordpress/

Side note when I’m just testing/debugging (like testing the above code) I do this within Debug Bar using Debug Bar Console because its quick and easy.

The output your looking for is something like this:

https://monosnap.com/file/TKWVUjKWRoWLcPdt8jvzqIeZ7tcPa5

That’s Debug Bar, using Debug Bar Console, outputting Kint… showing all of the properties for the EE_Event object in question, along with (which you’ll likely find more important) all of the available methods on that object.


jcforrester

February 19, 2024 at 1:53 pm

Thanks for the reply Tony!

Due to a filtering plugin complication, I ended up using the model in smaller capacity, looping through the events with WP_query and then targeting the model within the loop:
$slots = EEM_Event::instance()->get_one_by_ID( get_the_ID() );
And then
echo $slots->spaces_remaining();
And that got the job done for me.

That said, thank you so much for the responses, I would never have found that model without your help. You’re a rockstar.

Josh


Tony

  • Support Staff

February 20, 2024 at 2:35 am

I obviously don’t know the context in which this is used, but doing the above will at best increase the number of queries run on te page by the number of events being looped over, likely not a big issue but something to be aware of.

As your using WP_Query you may find EE has already injected the EE_Event object into the post object (yep, we do that automagically too but it depends on multiple factors). Within the look output the current post and look for an EE_Event object on the post.

$post->EE_Event

Whilst there’s nothing wrong with the code you posted, my advice would be to use a better variable name:

$slots->spaces_remaining();

Reading that in 6 months or 2 years from now… what is a $slot, something that calculates remaining slots? What else can I do with $slots?

For example:

echo $event_obj->spaces_remaining();

It just makes it a little more obvious without tracking the code what it is you’re working with.

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

Event Espresso