Apply a Discount Coupon Automatically

By | November 29, 2008

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:

[php]$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’);[/php]

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:

[php]tep_session_unregister(‘coupon_code’);
tep_session_unregister(‘coupon_amount’);
tep_session_unregister(‘coupon_type’);
tep_session_unregister(‘coupon_id’);[/php]

right before sorts that out. Now the full code to enable an autoamatically applied discount coupon is:

[php]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’);[/php]

This particular line of code would be changed depending upon which coupon you want to apply:

[php]$osC_Coupon = new osC_Coupon(‘test40’);[/php]

As you can see, in my admin area I have a couple of coupons to choose from:

So I could have used:

[php]$osC_Coupon = new osC_Coupon(‘FREEDEL’);[/php]

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.

4 thoughts on “Apply a Discount Coupon Automatically

  1. b2995

    My question is this… How would you allow customers to use additional coupons instead? I could like a discount amount to be automatically applied when a customer orders X number of products, but would also like to use additional coupons for promotional purposes at events, etc…

  2. Gary Post author

    More than 1 coupon leads to all sorts of problems with order_total and tax calculations. It can of course be done, but it’s not straightforward and you would need to pay someone to make it happen.

  3. b2995

    Hey Gary – I found a way around it… I combined the MSRP Contribution by wdept and the Quantity Price Break Per Product. By doing this, my customers can see that I’m initially discounting my prices thru the MSRP Addon. The QPBPP then shows and gives an additional discount for more than one item in a category purchased. Then I can still use a Coupon for promotions.

    Basically, what I had to do was take the amount I was offering (ex: $5 off 2 items), divide it ($2.50 off each item), and then list that price in the new QPBPP fields in Admin/Categories… I used the MSRP Addon to add my MSRP for calculations, but I used the QPBPP to display my discounts in the Product_Info page.

    🙂

  4. Gary Post author

    Cool – are you noticing any problems with Tax calculations?

Leave a Reply

Your email address will not be published. Required fields are marked *