Apply a Discount Coupon Automatically
I found a really interesting question at the osCommerce forum;
Does anyone know how i would have a certain discount coupon redeem automatically (for example when the customer logs in)? So that the user does not have to manually enter it anymore at checkout and sees his discount from the moment he logs in. This would be for a site-wide discount, available to every customer during a limited period of time.
The question is actaully aimed at users of the older contribution called "Credit Class/Gift Vouchers/Discount Coupons", I thought it might be interesting to see if it could be done in anyway using my version of Coupons...
2 minutes later, I'm pleased to say it does. A simple addition of the following lines of code in FILENAME_LOGIN (usually login.php) is all that it took:
-
$osC_Coupon = new osC_Coupon('test40');
-
$coupon_code = $osC_Coupon->coupon_details['coupon_code'];
-
$coupon_amount = $osC_Coupon->coupon_details['coupon_amount'];
-
$coupon_type = $osC_Coupon->coupon_details['coupon_type'];
-
$coupon_id = $osC_Coupon->coupon_details['coupon_id'];
-
tep_session_register('coupon_code');
-
tep_session_register('coupon_amount');
-
tep_session_register('coupon_type');
-
tep_session_register('coupon_id');
The coupon named "test40" is now automatically applied when logged in. I also had to make sure that any other coupon already entered by the customer was destroyed, so the addition of the following code:
-
tep_session_unregister('coupon_code');
-
tep_session_unregister('coupon_amount');
-
tep_session_unregister('coupon_type');
-
tep_session_unregister('coupon_id');
right before sorts that out. Now the full code to enable an autoamatically applied discount coupon is:
-
tep_session_unregister('coupon_code');
-
tep_session_unregister('coupon_amount');
-
tep_session_unregister('coupon_type');
-
tep_session_unregister('coupon_id');
-
$osC_Coupon = new osC_Coupon('test40');
-
$coupon_code = $osC_Coupon->coupon_details['coupon_code'];
-
$coupon_amount = $osC_Coupon->coupon_details['coupon_amount'];
-
$coupon_type = $osC_Coupon->coupon_details['coupon_type'];
-
$coupon_id = $osC_Coupon->coupon_details['coupon_id'];
-
tep_session_register('coupon_code');
-
tep_session_register('coupon_amount');
-
tep_session_register('coupon_type');
-
tep_session_register('coupon_id');
This particular line of code would be changed depending upon which coupon you want to apply:
-
$osC_Coupon = new osC_Coupon('test40');
As you can see, in my admin area I have a couple of coupons to choose from:

So I could have used:
-
$osC_Coupon = new osC_Coupon('FREEDEL');
instead. Probably this way of doing things could be applied to other Coupon contributions, I have no idea. If you try it, please let us know.
Easy as 123.


