New here? Read Greetings Earthling!

Tricks With Callbacks | Smart Coupon Sytem

Here's some food for thought.

I just integrated a coupons system in the new site and bumped into a bit of complexity challenge. I wanted to create something called like smart coupons that could set discount value dynamically based on buyer account age, buyer history, product age (discount for the first week and such), year period (holiday discounts) and so on. The complexity is not the problem ... it's just that I'm lazy and always looking for shortcuts.

If you are to config all these factors into a database ... you're in for a ride. As, the moment you set all these up, you may decide to make a few changes, add extra factors an realize this is a never-ending challenge.

So, when I created the coupon system I did not create a system featuring COUPON NAME & DISCOUNT VALUE/PERCENTAGE pairs. Instead I have COUPON NAME & CALLBACK pairs. That's right. I code the coupon directly in the database and I use the callback to generate the discount or issue an error.

Each CALLBACK will have access to live variables and will return a discount value or throw an Exception with an error text. In case I get an Exception in return I will issue an error message and if I get an amount I deduct it from the regular product price.

It is a very simple approach to a complex problem with unlimited variables. E.g.: I can create a coupon named PERSONAL and code different discounts for different users. And even code an expiry date inside. Or best: I can create custom coupons integrating an affiliate ID. So, if blogger X wants a special coupon I can give him one and he will be credited for any user [with no previous affiliate link clicked] buying with it.

After I did this trick I realized I can use callbacks for many other places where a static value will just not cover it / satisfy me. There's no such thing as exact solution for a problem and a callback can create dynamic responses to any problem based on its very attributes.

PS: I'll have a free detailed tutorial for this trick when I launch the new site. For now, those who understand can take advantage and those who already use this can make fun of me for being late.

Category: MySQL, PHP
Tagged: ,

One Response

  1. whoa! a whole new idea for a broad subject!
    will play with it soon, thank you for the heads up!