Support

Home Forums Event Espresso Premium REST API Payment amount credit/deduct points (cont'd)

REST API Payment amount credit/deduct points (cont'd)

Posted: August 3, 2022 at 11:55 am

Viewing 11 reply threads


He Zhengrui

August 3, 2022 at 11:55 am

Hi Tony,

Related to this topic
https://eventespresso.com/topic/rest-api-payment-amount-credit-deduct-points/

I have managed to set up Kint Debugger and I manage to pull out all the information that I need when I run examples like this:

d(EEM_Registration::instance()->get_one_by_ID(8647)-> get('STS_ID'));

However, I am unable to retrieve the current ATT_ID or any other array values for that matter in my function unless specified statically.

$user_id = mycred_get_user_id(EEM_Attendee::instance()->get_one_by_ID('5282')->get('ATT_email'));

Any advice?


Tony

  • Support Staff

August 3, 2022 at 4:48 pm

d(EEM_Registration::instance()->get_one_by_ID(8647)-> get('STS_ID'));

Is going to output a STS_ID through kint.

Try:

$registration = EEM_Registration::instance()->get_one_by_ID(8647);
d($registration);

Now you’ll see a LOT more information like what properties are set and the methods available to pull data.

However, I am unable to retrieve the current ATT_ID or any other array values for that matter in my function unless specified statically.

Can you explain this further, please?

Similar situation with this:

$user_id = mycred_get_user_id(EEM_Attendee::instance()->get_one_by_ID('5282')->get('ATT_email'));

Don’t do this all in one go, what if you want more than one detail?

$attendee = EEM_Attendee::instance()->get_one_by_ID(5282);
$user_id = mycred_get_user_id($attendee->email());

There are ‘getter’ methods for pulling in the email rather than using get() which will suit you more here.

Is that ATT_ID from the registration you pulled in earlier? Because you can pull the EE_Attendee object directly from the EE_Registration object using our models to make this easier.


He Zhengrui

August 7, 2022 at 10:57 pm

Thanks Tony! Let me try it and get back as soon as I can, thanks!


Tony

  • Support Staff

August 8, 2022 at 8:53 am

You’re most welcome.

Let me know and I’ll help more if needed πŸ™‚


He Zhengrui

August 8, 2022 at 10:01 am

Hi Tony,

Thanks for the reply and your suggestions are awesome;

To clarify, where are the other ‘getter’ methods in the documentation in the Model system? I’ve scoured through the entire documentation to no avail unfortunately :/

Is that ATT_ID from the registration you pulled in earlier? Because you can pull the EE_Attendee object directly from the EE_Registration object using our models to make this easier.

And how may I go about this?

Thank you Tony, I’m making great progress!


Tony

  • Support Staff

August 8, 2022 at 3:18 pm

To clarify, where are the other β€˜getter’ methods in the documentation in the Model system?

We don’t have every single method within our models documented which is why I recommend kint.

I’ll give you an example of how I use it so you can find various details.

A EE_Registration’s link everything together, the contact, the event, the ticket etc. So first lets say we need an EE_Registration with ID 425… this is how I’d get it:

https://monosnap.com/file/NNFJz1LjPfxEMx3pZS383XWy6qfoZy

Kint gives me a nice readable format, click on the EE_Registration object (never use the + sign, it opens all children… not good with out models as there can be a lot of them, just click on the name to open that level).

That gives you a breakdown of the objects properties:

https://monosnap.com/file/xFvmU7KNxMZZSTPexlArUQ7dspbCzL

What you need, are the available methods, there’s a tab for them:

https://monosnap.com/file/ShjjiwwbgMVulx9xAiYcg67oZRvItP

The EE_Registration object currently has 197 methods.

‘Getters’ and ‘setters’ are basically just methods used to ‘get’ or ‘set’ something (easy when you put that together πŸ˜‰ )

So to give an example there is a get_primary_registration() getter, which, returns the primary registration for a group:

https://monosnap.com/file/ShKAKoLZaDqrkoV7QmX7IH0UNLDX4h

You can go through and find all kinds of methods there and the names are pretty self-explanatory so in this case, if you are looking for get/set methods you are better looking over the method available and finding them by name rather than going through the documentation.

And how may I go about this?

You do the above πŸ™‚

What you’ll then find is an attendee() method, which returns the related EE_Attendee object.

https://monosnap.com/file/SX6COxMAAk9wcpFq8pqQEZYZ7OFiHL

Honestly, taking the time to learn how the models work will save you so much time if your going to be working with EE and custom code.

$registration = EEM_Registration::instance()->get_one_by_ID(8647);
if(! $registration instanceof EE_Registration) {
    return;
}
$attendee = $registration->attendee();
if(! $attendee instanceof EE_Attendee) {
    return;
}

At this point you know that $registration is an EE_Registration object and $attendee is the EE_Attendee object related to it. See how it becomes much easier just by knowing a little more above the models πŸ™‚

d($attendee);

Gives you all kinds of useful info: https://monosnap.com/file/sd54mvv3lJ0dzCrgsZgtyUTBBeE81i

164 methods on an EE_Attendee object, any related to email? lets check. Searching for email gives me 2 methods.

email(): https://monosnap.com/file/lxq1QcsjY7RmsMEpmKuNMJJGxl1yCH

Returns the attendees email address, that’s basically a getter for email and does get('ATT_email') in the background. But email() could more obvious, not so much in this use case, but the setters and getters take the logic away from what you are getting and jut give you it. You don’t need to know what ATT_email is…. email() gives you it!

It also gives you a setter, set_email(): https://monosnap.com/file/WzZ2XEPjKNVpBBqiLYboKoCYksvEAQ

To be honest, you shouldn’t be using that unless you need to change the email address of the attendee through code, which you shouldn’t. But as I’ve mentioned setters I might as well use the example.

$attendee->set_email('example@example.com');

Would set the email on that attendee to the value you just passed… thats a setter. Again, you didn’t need to know about ATT_email as the setter handles that for you etc.

So the idea here is to get familiar with the methods available and how the models work. Spending the time to get familiar with them will help you much more than reading the docs for specific methods.

There is more than enough info to get you familiar with them in the docs available: https://github.com/eventespresso/event-espresso-core/tree/master/docs/G–Model-System

But playing around with them to find what you need with a little bit of basic knowledge of them, helps πŸ™‚


He Zhengrui

August 10, 2022 at 12:58 am

Roger that Tony!

Thank you for such a detailed explanation and examples.

I will once again go with this new information and give it a try!

Cheers!


Tony

  • Support Staff

August 10, 2022 at 7:03 am

You’re most welcome πŸ™‚


He Zhengrui

August 18, 2022 at 4:36 am

Hi Tony,

at this juncture I have been working and experimenting with everything and it’s going smoothly;

one question though, how do I dynamically select the EEM_Registration object?

example:

$registration = <EEM Registration Object>;
if(! $registration instanceof EE_Registration) {
    return;
}
$attendee = $registration->attendee();
if(! $attendee instanceof EE_Attendee) {
    return;
}


He Zhengrui

August 18, 2022 at 5:48 am

I did some tinkering around and found $registration to use as EEM Registration object;

Therefore my code ends up as this:

function Rui_EE_add_cliquecoins($registration) 
{
	$rui_EE_registration = $registration;
	$rui_attendee = $rui_EE_registration->attendee();
	$rui_attendee_email = $rui_attendee->email();
	$rui_EE_ticket = $registration->ticket();
	$rui_EE_TKT_amnt = $rui_EE_ticket->price();
	
	$rui_mycred_amnt = intval($rui_EE_TKT_amnt * 10);

	mycred();
	$user_id = mycred_get_user_id($rui_attendee_email);
	mycred_add('EE Transaction', $user_id, $rui_mycred_amnt, 'Reward for TBC Course registration');
};

add_action('AHEE__EE_Registration__set_status__to_approved','Rui_EE_add_cliquecoins',10);


Tony

  • Support Staff

August 18, 2022 at 6:37 am

EEM_Registration is a model, which you use to return the EE_Registration object, my first example shows how you would use it:

$registration = EEM_Registration::instance()->get_one_by_ID(8647);

That’s if you already have the Registration ID, which generally you won’t. There are other ways to get the registrations based on the data you have, but no need for those currently.

Most of the hooks within EE related to a registration will pass the current EE_Registration object it is related to (like hook you are using above does πŸ™‚ )

Anyway, I’m glad you found a solution.

Just to provide some additional feedback on ‘improving’ your code, I don’t see any sense in doing things like this:

$rui_EE_registration = $registration;

Why are you assigning the registration to another variable with another name? Just use $registration πŸ™‚

If you really wanted the variable name for the EE_registration to be something different, declare it within the function parameters:

function Rui_EE_add_cliquecoins($rui_EE_registration)

Still not sure why you would need to do that, but that gives you the same thing without an additional variable.

Personally, I wouldn’t do this:

$rui_attendee_email = $rui_attendee->email();
...
$user_id = mycred_get_user_id($rui_attendee_email);

But just use the EE_Attendee object directly.

$user_id = mycred_get_user_id($rui_attendee->email());

Same with price, you don’t ‘need’ an additional variable:

$rui_EE_TKT_amnt = $rui_EE_ticket->price();

So you could use:

$rui_mycred_amnt = intval($rui_EE_ticket->price() * 10);

There’s no real harm in what you have now, just slightly more memory usage (which in this example would be almost nothing).


He Zhengrui

August 18, 2022 at 6:49 am

Thanks Tony for the feedback!

Yes I am in total agreement, my intention is to use the code and build upon it further as there are plenty more functions that I can utilize thanks to your resources πŸ™‚

I shall refine the code further πŸ˜›

Thanks for being awesome!

Viewing 11 reply threads

The support post ‘REST API Payment amount credit/deduct points (cont'd)’ 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