Support

Home Forums Event Espresso Premium PHP to automatically add transaction ID as a promo code

PHP to automatically add transaction ID as a promo code

Posted: May 5, 2021 at 5:23 am


ComedieTriomphe

May 5, 2021 at 5:23 am

Hi dear support,

I am currently working with my client to setup a system that will automatically create a promo code everytime a client buys a specific type of ticket (that we call a subscription). I want to use the purchase’s transaction ID which is unique and which we could display on their subscription confirmation email as a promo code.

Upon looking for a way to implement this, I first thought of using MySQL triggers but can’t because my webhosting doesn’t allow root access!
I am now looking into a way to implement this with PHP, which sounds like the best way.

I’ve read about your model system and use your flavour of WP_Query() to find the data I need to pull from the database, which works great. I can effectively get to the data I need, and could write a script that simply checks that transaction ID doesn’t exist into the promotion table, and create the row if it doesn’t.

What I am not sure about is where to run this script. Indeed, the subscription ticket is a special type of event that we currently made with the standard EE event type. Is there any best way you guys can think of where I could run this PHP code only when someone buys a ticket for an event of this type? (Maybe using an event category or such)

An alternative I am considering is building a plugin that will execute this periodically, but I’d much prefer if it were tied to the purchase.

Thank you for your suggestions!


Tony

  • Support Staff

May 5, 2021 at 7:41 am

Hi there,

There isn’t a way that you can run the code only for a specific event, but you can hook in and check for that event before actually doing anything.

The way I would do this is using this action hook:

do_action( 'AHEE__EE_Transaction_Processor__update_transaction_and_registrations_after_checkout_or_payment',
    $transaction,
    $update_params
);

That hook is fired whenever a transaction is updated during checkout or a payment is applied and an example of checking for a specific event can be found here:

https://gist.github.com/joshfeck/941ff81380f16e948245f8e0e6c985b6

That function first checks if the Transaction status is ‘Complete’ (‘TCM’) and then pulls the Event ID from each registration in the transaction and adds a capability, which is obviously not what you want but give you an idea of how to do it.

You could just use $primary_reg = $transaction->primary_registration(); to pull the primary registration and work with that. $update_params should also contain additional data you can use to determine if you want to create the promotion code on this request if needed.

I’ve previously created a promotion upload plugin which allows you to upload multiple promotions via CSV and again, whilst it’s not what you need it may help show how you can create promotions easily with the model system.

Take a look HERE from Line 320 onwards.


ComedieTriomphe

May 6, 2021 at 9:10 am

Hi Tony,

Thank you for your answer. I have implemented this partially. Here are a couples issues I am facing:

– What is PRC_ID, and is it important in my model object creation? Currently I leave it undefined and it just becomes 0 in the promotion table.
– What is the format for the datetime? I am asking because the end_date needs to be the transaction datetime + 2 years! Not sure how to go about this.
– Where are the names of the promotions showing up in the WP backend defined? I’d like to pass the name of the person who pays as the promotion name, and can’t see how to definite this in the Promotion table.

Thank you for your help!


Tony

  • Support Staff

May 6, 2021 at 3:25 pm

What is PRC_ID, and is it important in my model object creation? Currently I leave it undefined and it just becomes 0 in the promotion table.

PRC_ID is the ID of the price row assigned to the promotion.

If you have a PRC_ID assigned of 0, then you don’t have a price assigned to your promotion. esp_promotion stores details specific to the promotion but nothing around the price associated with that promotion (be it X value fixed/percentage etc)

Your promotion record needs a price for it to function and if you follow th code in the importer I linked to you can see how its set:

New price object created HERE.

New promotion object created HERE.

price assigned to the promotion HERE.

Without seeing your code I’m guessing you’ve missed that last line.

– What is the format for the datetime? I am asking because the end_date needs to be the transaction datetime + 2 years! Not sure how to go about this.

You should be able to use whatever DateTime format you want, as long as you tell the Model what that format is when you create the promotion. You can see how that’s done on L349 from above, HERE.

That line is expecting your Datetimes to be Y-m-d g:i a but you can change it if preferred.

Adding 2 years to now() can be done with something like:

date('Y-m-d g:i a', strtotime('+2 years'));

– Where are the names of the promotions showing up in the WP backend defined? I’d like to pass the name of the person who pays as the promotion name, and can’t see how to definite this in the Promotion table.

That’s your price name, PRC_name, again take not how that’s linked to the price object and not specifically the promotion which ties in with #1 above.


ComedieTriomphe

May 7, 2021 at 11:51 am

Hi Tony! That’s great, I didn’t realise the price was a necessity. All done now.

One last issue: my new promotion needs to be valid for some events only (usually category-wide). I can see how to assign the promotion to some events in the WordPress backend, but I don’t see how to do that from the database. Which table and which column assign a promotion to an event?

Thanks!


Tony

  • Support Staff

May 7, 2021 at 4:41 pm

One last issue: my new promotion needs to be valid for some events only (usually category-wide).

We do not currently have a category ‘scope’ within the promotions add-on, so the promotion will either need to be global (all events) or set on specific events. Note that if you create the promotion and then add more events into the category you are using you will also need to update the promotion to apply to the ‘new’ events.

Anyway, to answer your question, your looking for how to create EE_Promotion_Object‘s

This is how the event scope creates the promotion objects for selected events:

foreach ($selected_items as $EVT_ID) {
    if (in_array($EVT_ID, $evt_ids)) {
        continue;
    }
    $promotion_obj = EE_Promotion_Object::new_instance(
        array(
            'PRO_ID'   => $promotion->ID(),
            'OBJ_ID'   => $EVT_ID,
            'POB_type' => $this->slug,
            'POB_used' => 0
        )
    );
    $promotion_obj->save();
}

$evt_ids is an array of id’s saved from looping over the promotions earlier to see which ones already apply. Take a look at \eea-promotions\lib\scopes\EE_Promotion_Event_Scope.lib.php::handle_promotion_update() for more info.

POB_type will be ‘Event’ for each of those.

The support post ‘PHP to automatically add transaction ID as a promo 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.

Event Espresso