Support

Home Forums Event Espresso Premium Sending a PHP generated coupon to individual attendees

Sending a PHP generated coupon to individual attendees

Posted: May 16, 2017 at 3:53 am

Viewing 9 reply threads


deckchairuk

May 16, 2017 at 3:53 am

Hello!

I’m going some work on one of our sites and I’m trying to send a coupon code to certain users when they buy a ticket that allows them to have the first year of membership for free.

We’re using S2member which allows a coupon code to be generated using a PHP function. I was thinking I could use a custom message shortcode to generate the code and send it through the message templates.

The issue I’m having is I only want to send the codes to people that have purchased ‘Non-member’ tickets – but the only way I can see of doing this would be to base whether it’s sent or not on if the ticket title has ‘Non-member’ in it.

If I did this and got it working, I’d also need to make sure each registrant that had a non-member ticket bought would get their own unique code. Does the email run the shortcode each time per attendee or is there a chance it’d send the same code out to everyone?

I’ll do some testing but was hoping you might be able to shed some light on the issues.

Thanks, Harry.


Tony

  • Support Staff

May 16, 2017 at 4:18 am

Hi Harry,

You can’t use a custom shortcode to generate the code as the shortcode would generate a new code each time the message was generated (Yes, messages for a single attendee can be generated multiple times)

You could hook into the registration process, generate the code and then save the code to user meta during the process, then create a shortcode to pull the code from WP User assigned to the EE Contact for the registration when generating the message. However for that to work you users would need to be logged into the site before they registered onto the event.

Do you require users to log into the site before registering?


deckchairuk

May 16, 2017 at 4:22 am

Here’s a link to a GitHub gist to show my custom shortcode so far. See the comment for what I need to do next.

https://gist.github.com/deckchairuk/143da28fbde4efcfb86f297ccc2c3e76


Tony

  • Support Staff

May 16, 2017 at 4:39 am

The EE_Ticket_Shortcodes library has the get_ticket_set() method.

So you can get the ticket using $ticket = $lib->get_ticket_set(); within your check for EE_Ticket_Shortcodes

From there you have the EE_Ticket and do any kind of checking you need.

As mentioned above your generating the coupon code within a message shortcode means that a single user can generate multiple codes, you didn’t answer my question above so I don’t know if the above will work or not.


deckchairuk

May 16, 2017 at 5:08 am

Hey Tony,

Sorry I posted my reply without refreshing the page so hadn’t seen you’d replied so quickly.

No users won’t be logged in already as they will be using the discount code to create their membership.

I would want a new code to be generated each time as they will expire after use, so using shortcodes should work right?

Thanks for the information on the get ticket set, I will see if I can wrap a conditional around the code now.

Thanks, Harry.


Tony

  • Support Staff

May 16, 2017 at 5:12 am

I would want a new code to be generated each time as they will expire after use, so using shortcodes should work right?

So you want a new code generated if you click to resend the messages to the same registrant?

If you use the shortcode to generate a code, a new code will be generated every time that shortcode is ran, regardless if it’s a new registration or a previous registration that you resending the messages to.


deckchairuk

May 16, 2017 at 5:18 am

Ah sorry, I misunderstood. No that wouldn’t be ideal. I’m not sure how much of an issue this would be as it’s rare that messages would be re-sent.

I will get the conditional in based on the help you’ve given me and do some testing.

I may be back with a couple more questions – sorry to be bothering you on something so niche.

Thanks, Harry.


deckchairuk

May 16, 2017 at 5:36 am

So I’ve been trying to get the conditionals to work but my new-to-oop brain is having troubles with protected classes.

While trying to get the ticket name, it’s saying the property is protected and I’m not allowed to use it.

Am I doing something incorrectly?
https://gist.github.com/deckchairuk/143da28fbde4efcfb86f297ccc2c3e76


Tony

  • Support Staff

May 16, 2017 at 6:21 am

EE uses getters and setters, so you pull details through methods rather than the fields directly.

In the case of name you’d use…. name()

So $ticket_name = $ticket->name();

I’d recommend looking through the EE_Ticket.class.php for the of available methods for the ticket class.

Or better yet use Kint and pull a ticket in by ID, wrap it within d(); and veiw the output, then do some digging to find what you need.

$ticket = EE_Registry::instance()->load_model('Ticket')->get_one_by_ID($id);
d($ticket);

(you can’t do that within your shortcode parser as you won’t see it you’ll need to use Debug Bar Console or load the code somewhere else)


deckchairuk

May 16, 2017 at 6:37 am

Ah perfect. Thanks for explaining that 🙂

I will have a go at getting it working now.

Thanks again, Harry.

Viewing 9 reply threads

The support post ‘Sending a PHP generated coupon to individual attendees’ 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