Support

Home Forums Event Espresso Premium Get Attendee Details like State/Province in custom functions

Get Attendee Details like State/Province in custom functions

Posted: June 4, 2020 at 9:37 am


Matt

June 4, 2020 at 9:37 am

How do I hook into the primary attendee’s details so that I can get their province/state? I can get Items with $this->get_items() but not sure how to get attendees.

Thx
Matt


Tony

  • Support Staff

June 4, 2020 at 4:37 pm

Hi Matt,

May I ask, from where?

What objects do you have available already?

I can get Items with $this->get_items() but not sure how to get attendees.

In this example, $this must be an EE_Line_Item, likely a grand total line item but that wont really help here.


Matt

June 5, 2020 at 4:51 am

Ok. For more context, last year we built a regional tax tool into the system which is a combination of custom functions and a change in EE_Line_Item. This was a bit tricky, but a workaround because there’s no other way to manage Canadian Provincial Taxes on a ticket-by-ticket basis. In this case, we just load in all Canadian Provincial Taxes and then put a Province code in the Ticket Description to choose which one you want. It requires a few things, but the basic code looks like this:

foreach ($this->get_items() as $item) {
$province = substr( $item->desc(), 0, 2 );
$tax_totals[$province] += $item->total();
}

Coupled with a loop through each tax type and:
$total_on_this_tax = $tax_totals[$tax->desc()] * $tax->percent() / 100;

Then it recalculates.

ANYWAY…With everything moving online, we now need to calculate tax based on user location for all events with “Online” in the title. So I want to target and replace the use of $item->desc() with something like $primary_attendee->state() but don’t know how to get access to the object from inside EE_Line_Item.

Does that help explain it better?


Tony

  • Support Staff

June 5, 2020 at 6:46 am

Thanks, that gives me some context of your code but not where your running it.

How are you hooking into Event Espresso? Which hook?

It’s possible to pull the primary attendee from the current session data, or even line items but it can get a little messy and you’ll need to jump through a lot of hoops. So I’m trying to figure out if there are other objects available to you at the time you’re running your code.


Matt

June 5, 2020 at 6:56 am

Not sure I fully understand this question. I’m using the Multi Event Registration add-on primarily. We’re rewriting the recalculate_taxes_and_tax_total function a bit with this. Not sure what objects are available there.

I might also not know what my options are for “Which hook?”.

If there isn’t an easier way, pulling from the Session is fine too. Can you tell me how to do that? I get nothing from $_SESSION.


Tony

  • Support Staff

June 5, 2020 at 7:56 am

We’re rewriting the recalculate_taxes_and_tax_total function a bit with this.

Ok, so your not using any hooks/hooking into EE at all but hacking up core files.

If you change core files you’ll then need to reapply those changes after each update to Event Espresso and it prevents us from providing support as your now running a modified version of the plugin.

Rather than doing that you would be better requesting a hook is added to the function so you can hook in and change what you need without modifying core. The best way to do that is to open up a pull request which will be reviewed by the dev team and if no issues raised, included within core.

Not sure what objects are available there.

None, because you’re within the EE_Lint_item class so you would need to use the lines items.

If there isn’t an easier way, pulling from the Session is fine too. Can you tell me how to do that? I get nothing from $_SESSION.

I’m not entirely sure this will work within that class but here is an example of how to pull the primary_registration from the current session, and then the attendee object from that:

https://gist.github.com/joshfeck/3457c021e95182fed3de311a5f59d894#file-eea_is_track_guests_to_primary-php-L6-L13

Once you have an EE_Attendee object, you can use:

$attendee->state()

Which will return either the state abbreviation or state name, you can also pull the state object if you need more details.

$state_obj = $attendee->state_obj();


Matt

June 5, 2020 at 9:30 am

I get it. We needed a solution and after many support requests, couldn’t get to the bottom of it. You are right, we have to backup and reinstall our fix for every update. Do you know if this type of Regional Tax support is coming soon? It feels like anyone doing a tour that crossed state or provincial lines would need this.

Anyway, I’m able to get the EEM_Attendee instance to give me a result and if I output the array I do see the STA_ID and other details buried in there under [_entity_map:protected][1][####][_fields:protected]

Can you tell me the command to get these details into a variable I can use? I tried the state and state_obj you suggested and can’t get a result. Also, I’m just getting the ID number, can you tell me how to convert that into a province code (ie. “ON” or “AB”) or if that’s not possible the province name?

Thanks,
Matt


Tony

  • Support Staff

June 5, 2020 at 9:46 am

I get it. We needed a solution and after many support requests, couldn’t get to the bottom of it.

Yeah I went thought your previous support threads and noticed you’ve asked for this previously, however you’ve still chosen what would be considered the ‘wrong’ way to mod code here.

You could have added your own hook into the above function to to whatever you need to do in a PR and then never had to re-apply your modifications again as you would have been using the hook.

Now, the line item system in EE isn’t my area of expertise so there may actually already be a hook available for this, or a different method to do it without overriding that function which is why I recommend creating a PR as during review the dev who reviews the code may provide feedback on a ‘better’/exsiting way to do it.

Do you know if this type of Regional Tax support is coming soon?

It is not something we are currently working on so I can say it’s wont be added to EE ‘soon’. Right now we are working on changes to the event editor and then recurring events.

Anyway, I’m able to get the EEM_Attendee instance to give me a result and if I output the array I do see the STA_ID and other details buried in there under [_entity_map:protected][1][####][_fields:protected]

On the attendee object itself?

Use the Kint Debugger Plugin and wrap it in d(); for a much nicer output, it will also show you all of the method available on that object.

(Word of warning thought, when you click on the + sign with kint it recursively opens ALL child elements, thats a LOT of elements with EE objects and it can crash your browser. The solution to that is to not click on on the + and just the blue bars to open each section as you need it).

Can you tell me the command to get these details into a variable I can use? I tried the state and state_obj you suggested and can’t get a result.

state() and state_obj() are the methods you need to use.

Can you post your code to PasteBin or similar so I can take a look?

Also, I’m just getting the ID number, can you tell me how to convert that into a province code (ie. “ON” or “AB”) or if that’s not possible the province name?

The above methods wouldn’t give you just and ID, where are you getting the ID from as you mentioned you weren’t getting results from the above?


Matt

July 8, 2020 at 9:38 am

Thanks for the Kint reference. That is very helpful.

I’ve used the following to get the details when I’m on the “Review your order” page.

$checkout = EE_Session::instance()->checkout();
d($checkout)

In the Kint output it shows public primary_attendee_obj but I can’t get at that. Can you tell me how to access that variable because that would solve everything for me. It contains STA_ID in it, so that’s all I’d need to get at.

Thanks,
Matt


Tony

  • Support Staff

July 8, 2020 at 10:39 am

Hi Matt,

I showed you how to get the primary attendee obj in my previous reply here:

https://eventespresso.com/topic/get-attendee-details-like-state-province-in-custom-functions/?view=all#post-312873

Pull the transaction from the checkout, the primary_registration linked to the transaction and then the EE_Attendee linked to the primary registrant.

The support post ‘Get Attendee Details like State/Province in custom functions’ 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