Support

Home Forums Event Espresso Premium limiting the member user to buy discounted tickets (has_unique_invitation_code)

limiting the member user to buy discounted tickets (has_unique_invitation_code)

Posted: July 30, 2018 at 6:10 am


RashminNMM

July 30, 2018 at 6:10 am

Hello,

I read one of the support forum questions and I wanted to limit the member users to buy the member-only ticket. Now –
1) I have limited them to buy 2 tickets for members.
2) Have add this capability to ‘subscriber’ role.
3) Have inserted the has_unique_invitation_code in the ticket.
4) Also inserted the plugin via how to add site specific plugin to insert the code to remove the has_unique_invitation_code.

But still I am able to select the member ticket when I log in again which shouldn’t allow.
Any help would be appreciated.
Regards
Rashmin


Tony

  • Support Staff

July 30, 2018 at 2:44 pm

Hi there,

First the basics.

Have you confirmed that the custom functions plugin you say you added the code to has been activated on the site?

Go to Dashboard -> Plugins and confirm that the custom function plugin is activated there (not just installed).

With the WP User integration add-on active, if you have done this:

3) Have inserted the has_unique_invitation_code in the ticket.

Then it means the capability is not being removed from the user account, EE just checks if the current_user_can({ticket_capability}) so if you still have access to the ticket, you still have that cap on your account.

If can you confirm and check the account in question, does it still have the above cap?


RashminNMM

July 30, 2018 at 3:14 pm

Hello,

– Yes the plugin is activated. but how do I check if its executed?
– I executed the steps in the following order –
1) With the plugin user role editor, added a capability has_unique_invitation_code and assigned it to a subscriber role.
2) WP user integration add-on active, while creating the Cooking workshop event, created an event and inserted capability has_unique_invitation_code for member tickets. Included max tickets as 2.
3) created a new user with subscriber role.
4) with this new user, bought 2 cooking workshop tickets. logged out
5) Logged in again as the same user and I can still purchase the member ticket (which it shouldn’t allow).
I hope I followed the steps correctly.
Any inputs?

Regards
Rashmin


RashminNMM

July 30, 2018 at 3:16 pm

Hello ,

I checked the user account and still it does have the cap. how do I know that the plugin has been executed?


Tony

  • Support Staff

July 30, 2018 at 3:25 pm

4) with this new user, bought 2 cooking workshop tickets. logged out

How far through the registration did you go?

All the way to the end and triggered the emails?

Paid or free tickets?

how do I know that the plugin has been executed?

You could add some code to write to a log file within the function, if the log file has whatever you set in that function, the code ran.


RashminNMM

July 30, 2018 at 3:35 pm

I completed the registration until the end in both case 4 and 5. received emails and receipt as well. These are paid tickets.

What code that be? I used the exact code snippet for removing the capability you mentioned in https://gist.github.com/joshfeck/51c2ef122616b48cc4846b1f44384859

thx


Josh

  • Support Staff

July 30, 2018 at 7:41 pm

Hi Rashmin,

What you’re seeing is actually expected because in the above steps, you added the capability to the user role.

1) With the plugin user role editor, added a capability has_unique_invitation_code and assigned it to a subscriber role.

Instead of editing the subscriber role, you actually need to edit the individual user account to add the cap. The source of the WP_User::remove_cap() helps show why:
https://developer.wordpress.org/reference/classes/wp_user/remove_cap/#source

So when you add the cap to the role, then WP will remove the cap from the user, then add the cap back because it merges in all caps from the user role.

If you have a lot of user accounts that need the custom cap added, the second part of that gist includes a sample plugin you can use to programmatically add the custom cap to the user accounts:
https://gist.github.com/joshfeck/51c2ef122616b48cc4846b1f44384859#file-wp_add_user_caps_on_activate-php


RashminNMM

July 31, 2018 at 1:32 am

Hello,

Great. its working now. Thanks for the explanation.
I do not have many or any users a we will be implementing the user functionality from 1st August. So happy that its working now.

1) But I still have another query, with this remove cap, will work for 1 event.For every new event we organise, I need to add this (has_unique_invitation_code) to the user base again, am I right?
2) If have 2 different event registration available at a given time with the member discount. The member user registers for 1 event (the Capp is removed). But then for 2nd event he should be still allowed to purchase member tickets. Then having different capability role in the ticket for each event is a solution?

Thanks for your help


Josh

  • Support Staff

July 31, 2018 at 6:35 am

I need to add this (has_unique_invitation_code) to the user base again, am I right?

It will work better to have a unique cap for each event where you’re restricting the tickets in this manner, and update your custom code accordingly.

If have 2 different event registration available at a given time with the member discount. The member user registers for 1 event (the Capp is removed). But then for 2nd event he should be still allowed to purchase member tickets. Then having different capability role in the ticket for each event is a solution?

I think you’re on the right track, but please remember you’re not dealing with user roles. Rather, it’s user accounts and adding/removing capabilities from those accounts.


Tony

  • Support Staff

July 31, 2018 at 6:40 am

1) But I still have another query, with this remove cap, will work for 1 event.For every new event we organise, I need to add this (has_unique_invitation_code) to the user base again, am I right?

Correct.

2) If have 2 different event registration available at a given time with the member discount. The member user registers for 1 event (the Capp is removed). But then for 2nd event he should be still allowed to purchase member tickets. Then having different capability role in the ticket for each event is a solution?

With the current code, when they register onto eithr event the cap is remove, so you’d need a function to check for both caps.

However, what I would do, is use a function to pull the capability from the ticket linked to the registration, check if the user had that cap and remove it.

Replace Josh’s first example with this:

https://gist.github.com/Pebblo/25cab2c24fe28b0c65285b15f0e25104

Then for every event you want to use a unique capability, you can add the event name, or ID to the cap, for example – has_unique_invitation_code_event_111

So you’d add that cap to your user, add the capability to the specific ticket for that event and the function will remove whatever cap is set on the ticket.

So your users may have has_unique_invitation_code_event_111 and has_unique_invitation_code_event_222 on their account. Registering on a ticket with either of those capabilities removes THAT capability.

Make sense?


RashminNMM

July 31, 2018 at 10:41 am

Yes. got it.
As of now for the very first implementation we will be having only 1 event in the coming month. But in the future, its good to know, how to handle more than 1 event registrations at the same time.
Thanks


RashminNMM

August 3, 2018 at 9:36 am

Hello ,

I thought I wouldn’t have to use Tony’s code in the above message as we are having only 1 upcoming event. But for 1 event we have different ticket types wherein depending on the member user, need to display all the member tickets of only 1 ticket type.
Hence I need to implement the above logic having different capability for 2 different tickets in the same event.
So I followed the above steps, butI guess the plugin isn’t doing anything.

I deactivated the existing plugin, deleted/removed the old folder & file using the FTP account and reloaded the plugin with same name with the changed code. But the capability is not being removed and I have a feeling that the plugin is not executed.
Is there any specific step to follow to change update the custom specific plugin ?


Josh

  • Support Staff

August 3, 2018 at 4:48 pm

Hi Rashmin,

The code example needed a few fixes. Here’s an updated example:

https://gist.github.com/joshfeck/ad9d682bf56880eeb7d489ed9c16c93f


RashminNMM

August 4, 2018 at 9:59 am

Hello Josh,

The code with removing single event capability worked well. But the latest code you gave yesterday still doesn’t remove the capability from the user after registration.
In my scenario, I have 3 capabilities set and assigned to 3 different tickets in a single event. Depending on the user the respective ticket should be active.

Rashmin.


Tony

  • Support Staff

August 6, 2018 at 3:16 am

What are the capabilities you have set on the ticket/user?

The above snippet will only look for capabilities that begin with has_unique_invitation_code.


RashminNMM

August 6, 2018 at 6:22 am

Hello Tony,

Yes I have set the capability has_unique_invitation_code followed by event_student or event_family depending on the ticket. Depending on the kind of membership taken, I have added respective capability to the user.

Regards
Rashmin


Tony

  • Support Staff

August 6, 2018 at 7:32 am

Yes I have set the capability has_unique_invitation_code followed by event_student or event_family depending on the ticket.

As mentioned:

The above snippet will only look for capabilities that begin with has_unique_invitation_code.

The code will only remove a capability from a user if the capability contains the text has_unique_invitation_code. So, you posted this:

The code with removing single event capability worked well. But the latest code you gave yesterday still doesn’t remove the capability from the user after registration.

What code are you referring to? The code I posted should remove the capability from the user as long as the capability contains the text has_unique_invitation_code. We can’t just remove every cap if a ticket has one as there may be cases when a cap shouldn’t be removed, requiring specific text is used in the capability works around that.


RashminNMM

August 6, 2018 at 12:41 pm

I guess I wasn’t clear.
The following code for removing the capability – https://gist.github.com/joshfeck/51c2ef122616b48cc4846b1f44384859 worked fine.but I have a situation where I want different capability removal.
But https://gist.github.com/joshfeck/ad9d682bf56880eeb7d489ed9c16c93f to dynamically remove the current registrations ticket capability (if set) from the current userid isn’t working in my situation.
I have defined 3 capabilities –
1) has_unique_invitation_code_event_family
2) has_unique_invitation_code_event_student
3) has_unique_invitation_code_event_individual
– depending on the type of user, one of the capability is set for that userid.
– I have defined 3 different type of tickets in a single event and assigned respective capability to the family, student or individual ticket.
So the user with that capability is able to register for that type of ticket. Once he/she has registered for the membership tickets, it shouldn’t allow them to buy these tickets again.
In my case all 3 tickets in a single event, have the capability text containing ‘has_unique_invitation_code’ string.
Still the code is not working in my case. Have I missed anything here?

Thanks
Rashmin


Tony

  • Support Staff

August 7, 2018 at 3:54 am

Ah, ok, I follow now.

I’ve just gone through and tested both mine and Josh’s example with the dynamic cap removal and both work (mine had a typo which has now been fixed).

I set up 3 tickets similar to yours – http://take.ms/18yeA

Then register onto 2 of them individually, when I return to the ticket selector it has removed the caps – http://take.ms/OdTpC

If you enable WP_DEBUG using this code in your wp-config.php file:

https://eventespresso.com/wiki/troubleshooting-checklist/#wpdebug

Do you see any error sin /wp-content/debug.log?


RashminNMM

August 7, 2018 at 11:32 am

Hello Tony,
Yes now its working with your revised code. Thanks a lot.
Now I have yet another query !! We have 3 membership types (student, individual and family). For all the events we organise, we have 3 different tickets.
– Adult ticket
– Child ticket
– Student ticket
https://cl.ly/3f3Y1l1i1j21
For student and individual membership there is a one-to-one match with user capability and the capability set at the ticket level.
Student membership – has_unique_invitation_code_event_student
Individual membership – has_unique_invitation_code_event_individual
The problem comes when an user has family membership. He/she can choose
– adult ticket (has_unique_invitation_code_event_individual at ticket level)
as well as
– child ticket (has_unique_invitation_code_event_family at ticket level)
So when a user with family membership ( I assign both the capabilities at user level), the code removes capability for only 1 ticket for that registration.
Is there a way to remove cap for having the string ‘has_unique_invitation_code’ in all the tickets for that 1 registration?

Or can I set it up in a different way to avoid this?

Thanks
Rashmin


Tony

  • Support Staff

August 8, 2018 at 3:26 am

I’ve added another example to my gist that pulls in all of the registrations, then loops over them all and pulls the ticket and the cap set for that ticket, then removes them from user.

Try this one:

https://gist.github.com/Pebblo/25cab2c24fe28b0c65285b15f0e25104#file-example-multiple-caps-php

Note additional queries are needed to pull in all of these additional details, nothing major, just something to be aware of.


RashminNMM

August 8, 2018 at 5:33 am

Thanks a lot Tony.
Worked smoothly and as we wanted it to work.

Regards
Rashmin.


Tony

  • Support Staff

August 8, 2018 at 5:46 am

You’re most welcome πŸ™‚ I’m glad that worked for you.

If have a minute we would really appricate if you could share your experience with Event Espresso or provide a review on WP.org.

The support post ‘limiting the member user to buy discounted tickets (has_unique_invitation_code)’ 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.

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: closed

Updated by  Tony 4 months ago ago

Topic Tags

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!