Support

Home Forums Event Espresso Premium Get Number of Tickets Purchased By Attendee Per Event

Get Number of Tickets Purchased By Attendee Per Event

Posted: May 9, 2016 at 4:22 pm


Josh

  • Support Staff

May 9, 2016 at 8:41 pm

There’s an example in EE core that you can follow where in the EE_Cart class there’s a all_ticket_quantity_count() method. It returns the total quantity of tickets in the cart.


kellyjo

May 10, 2016 at 4:57 pm

Hey Josh,

I did a search for how to apply it and I’m not sure if I’m calling it correctly.

I’ve tried your information from this post:

https://eventespresso.com/topic/getting-total-amount-of-tickets-in-cart/

I attempted without and then ended up trying it with this code the header.php:

<?php EE_Registry::instance()->load_core( 'Cart' ); ?>

Neither way worked.

In the custom functions.php credit code:

$ticketcount = EE_Registry::instance()->CART->all_ticket_quantity_count();

Nothing is happening at all. No error and no change in the amount of credits. Do I need to load an object instance of something else?


Josh

  • Support Staff

May 11, 2016 at 10:51 am

In this case, you wouldn’t call the load_core() method in header.php, instead you can call the method in your function file.


kellyjo

May 11, 2016 at 5:32 pm

It still for some reason is just returning 0 as the total tickets Josh, even though I have 2 tickets in the cart. I would think that $ticketcount variable would be sufficient enough.


Josh

  • Support Staff

May 11, 2016 at 8:25 pm

You might be calling the method too late and the cart is empty at that point then.

Instead, since the $payment object is passed to the AHEE__thank_you_page_payment_details_template__after_each_payment
action hook, you can do something like this:

$payment->transaction() instance of EE_Transaction ? $payment->transaction()->count_related('Registration') : 0;

You’ll need to be sure to pass in the $payment object when you call the function like this:

add_action( 'AHEE__thank_you_page_payment_details_template__after_each_payment', 'my_function' );
function my_function( $payment ) {
  // function code here
} 


kellyjo

May 11, 2016 at 9:31 pm

I passed in the $payment object and used the code you provided, but it did not increase the credits in the usermeta at all.


Josh

  • Support Staff

May 12, 2016 at 7:53 am

It might help to know the context of how you used the code I provided. If you put the same code in a function and return it, it does return the number of tickets in the transaction, so the issue is likely in your code.


kellyjo

May 12, 2016 at 11:32 am

No problem Josh. The code is in the custom functions.php plugin.

Here is the full code:


//Update Credits For Users Upon Payment
function ee_update_credits( $payment ) {
        $user_id = get_current_user_id();
        $credits = get_user_meta($user_id, 'credits', true);
        $ticketcount =  $payment->transaction() instanceof EE_Transaction ? $payment->transaction()->count_related('Registration') : 0;
        $credits = $credits + $ticketcount;
        update_user_meta($user_id, 'credits', $credits); 
    }

add_filter ('AHEE__thank_you_page_payment_details_template__after_each_payment', 'ee_update_credits');


Josh

  • Support Staff

May 12, 2016 at 12:26 pm

It’s adding credits when I try the code out on my site. The issue I see with the code in its present state though is it will add more credits each time the thank you page is loaded or refreshed. You might try the

AHEE__EE_Transaction_Processor__update_transaction_and_registrations_after_checkout_or_payment

hook instead. This will help ensure that the credits are added only one time per transaction.


kellyjo

May 12, 2016 at 10:59 pm

I wish I had good news, but adding that hook breaks the template for me and I’m just left with an infinite spinning wheel.

I added an is_page check instead and if need be I’m sure I can add other parameters, but unfortunately, the code is just not working for me at all.

Now the only other thing I can think of why it is not working: I am putting in a discount code which gives me a 100% discount on priced ticket. So technically, the amount of the total cost of the ticket is $0. However, I would think that we are just counting registrations for the event in that transaction, not the actual amount, from the looks of the code.


kellyjo

May 13, 2016 at 2:33 am

Hey Josh,

This issue has been resolved and I have the credit system working for now. I went around EE4 and looked into what was on the pages themselves. I came up with a creative solution for this.

To sum up the idea of how it works:

There is a “Thank You” page where EE4 gives information about the purchase (Registrant Name, REG Code, and REG Status). On that thank you page are some links including “edit info” and “resend email”. There are also 2 additional links: ?e_reg_url_link and ?token=1 which is present twice). Anything more than those 2 links are actual registrations, which are the “resend email” links visible on the page.

What I did was create a function to determine the amount of links on the page using the WordPress wp_extract_urls function and then subtracted those 2 extra links to get the total amount of registrations.

I created a hidden meta field which turns off allowing credits only on this page, so that if someone refreshes the page, they cannot just keep getting credits, and obviously, the only way back to this page is by purchasing a ticket. They would have to leave the page in order to re-enable their ability to get more credits.

As far as clicking on that edit info button and returning, there is another hidden meta field that holds temporary credits and if someone lands on the “Registration Checkout” page and those temporary credits exist, it is deducted from their user meta data until they land back on the Thank You page again. If they land on any other pages, those temporary credits are set to 0 so nothing gets deducted.

The support post ‘Get Number of Tickets Purchased By Attendee Per Event’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso