Posted: 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’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!
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:
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:
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
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.
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.
Thank you for your help!
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.
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.
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
Adding 2 years to now() can be done with something like:
That’s your price name,
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?
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
This is how the event scope creates the promotion objects for selected events:
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.