Support

Home Forums Event Espresso Premium Recording the number of tickets purchased by a user per event to database

Recording the number of tickets purchased by a user per event to database

Posted: November 28, 2017 at 5:40 pm

Viewing 3 reply threads


REAIE

November 28, 2017 at 5:40 pm

I’m attempting to place a limit on the number of tickets per event a unique user can purchase. Currently EE’s Ticket Limit feature works on a per transaction basis, allowing users to return to the page after ordering and simply order more discounted tickets. So far to record how many tickets a user has purchased I’ve written

function record_order(){
    $checkout = EE_Registry::instance()->SSN->checkout();
    if ( $checkout instanceof EE_Checkout ) {
        $transaction = $checkout->transaction;
        if ( $transaction instanceof EE_Transaction ) {
            foreach ( $transaction->registrations() as $registration ) {
                if ( $registration instanceof EE_Registration ) {
                    $event = $registration->event();
                }
			}
		$user_id = get_current_user_id();
		$event_id = $event->ID();
		$tickets_purchased = ?
		$wpdb->insert('ticket_purchases', array(
			'user_id' => $user_id,
			'event_id' => $event_id,
			'$tickets_purchased' => $tickets_purchased	
		));
        }
    }
}

However as you can see when I do a var_dump on $transaction I can’t find the number of tickets being purchased. Also what would be the best action hook to use for this?


Tony

  • Support Staff

November 29, 2017 at 4:56 am

Hi there,

Whilst I understand what you are trying to do, the code you’ve posted won’t work if you are using Mutli Event Registration (MER for short) and you add more events to the cart.

foreach ( $transaction->registrations() as $registration ) {
    if ( $registration instanceof EE_Registration ) {
        $event = $registration->event();
    }
}

Loops over all of the registrations and sets $event to be the EE_Event object for each one, meaning $event will always be the event that is assigned to the last registration. If you using MER and add tickets from multiple events, the last registration may not be from the same event as the others.

If you’re not using MER then your looping over all of your registrations and pulling the same event each time, not a big deal but pretty inefficient.

Assuming you’re not using MER I’d just pull the primary registrant from the transaction:

$primary_registration = $transaction->primary_registration();

Check you have an instance of EE_Registration and pull the event from that.

if ( $primary_registration instanceof EE_Registration ) {
        $event = $primary_registration->event();
}

If for some reason you don’t have a EE_Registration object all of your code will continue to run (and fail) because it is outside of that check, move that into the instance of check.

If all you need is the event ID and the number of tickets purchased for that specific event (not the qty of each specific ticket type) then you can pull that from the EE_Registration object using group_size(), so you could have something like this:

$primary_registration = $transaction->primary_registration();

if ( $primary_registration instanceof EE_Registration ) {
    $event = $primary_registration->event();
    $tickets_purchased = $primary_registration->group_size();

    if( $event instanceof EE_Event ) {
        $user_id = get_current_user_id();
        $event_id = $event->ID();
        $wpdb->insert('ticket_purchases', 
            array(
                'user_id' => $user_id,
                'event_id' => $event_id,
                'tickets_purchased' => $tickets_purchased  
            )
        );
    }
}

Can I ask why you’re not using user meta to store this?

If your using MER it gets more complicated, you’ll need to loop over each registration and pull its event, add that to an array and then set how many tickets for each event you have within an array on the event element, menaing your using a multidimensional array. After looping over all registration, assigning all ticket qty’s to each event you then loop over the array you created and add save the details.

Also what would be the best action hook to use for this?

It all depends on when you want to record the tickets, as soon as the user selects the tickets and adds registration details? When the registrations are paid/approved?

You would be better opening up an issue, labeling it a question and providing more details on how/when you want this to work. One of our developers may be able to point you in the right direction from there.

Note – we do not provide support for custom code, whilst we are usually more than happy to help point you in the right direction (as above) it should be considered untested and unsupported.


REAIE

December 4, 2017 at 6:41 pm

Thanks for the detailed reply! I’ve opened an issue on GitHub. Not using MER, is there an alternate to the group_size method that I can use to get the qty of each specific ticket type being purchased?


Tony

  • Support Staff

December 5, 2017 at 1:51 am

I don’t think there is a function you can use to pull the qty of all ticket types, you would need to loop over each registration and count the tickets for each type of ticket you find (a little similar to how you would need to work with MER above).

If you include that question in your issue above the developers can point you in the right direction.

Viewing 3 reply threads

The support post ‘Recording the number of tickets purchased by a user per event to database’ 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