About Club osCommerce

Showcasing osCommerce…the good, the bad and the ugly!

Follow osc_pro on Twitter

Apply a Discount Coupon Automatically

Written By Gary on Nov 29 2008 · Comments (4) Follow osc_pro on Twitter

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:
  1. $osC_Coupon = new osC_Coupon('test40');
  2.         $coupon_code = $osC_Coupon->coupon_details['coupon_code'];
  3.         $coupon_amount = $osC_Coupon->coupon_details['coupon_amount'];
  4.         $coupon_type = $osC_Coupon->coupon_details['coupon_type'];
  5.         $coupon_id = $osC_Coupon->coupon_details['coupon_id'];
  6.         tep_session_register('coupon_code');
  7.         tep_session_register('coupon_amount');
  8.         tep_session_register('coupon_type');
  9.         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:

PHP:
  1. tep_session_unregister('coupon_code');
  2.   tep_session_unregister('coupon_amount');
  3.   tep_session_unregister('coupon_type');
  4.   tep_session_unregister('coupon_id');

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

PHP:
  1. tep_session_unregister('coupon_code');
  2.   tep_session_unregister('coupon_amount');
  3.   tep_session_unregister('coupon_type');
  4.   tep_session_unregister('coupon_id');
  5.   $osC_Coupon = new osC_Coupon('test40');
  6.   $coupon_code = $osC_Coupon->coupon_details['coupon_code'];
  7.   $coupon_amount = $osC_Coupon->coupon_details['coupon_amount'];
  8.   $coupon_type = $osC_Coupon->coupon_details['coupon_type'];
  9.   $coupon_id = $osC_Coupon->coupon_details['coupon_id'];
  10.   tep_session_register('coupon_code');
  11.   tep_session_register('coupon_amount');
  12.   tep_session_register('coupon_type');
  13.   tep_session_register('coupon_id');

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

PHP:
  1. $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:

PHP:
  1. $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.


Buy Gary A Beer?
Buying me a "beer" helps me to keep my contributions updated and keep this blog alive - and you get a link from my homepage to your site. Cheers!


4 Comments

  1. 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…

    Comment by b2995 — April 16, 2009 @ 10:23 pm


  2. 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.

    Comment by Gary — April 17, 2009 @ 8:03 pm


  3. 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.

    :)

    Comment by b2995 — April 19, 2009 @ 4:59 pm


  4. Cool – are you noticing any problems with Tax calculations?

    Comment by Gary — April 19, 2009 @ 5:59 pm


Leave a comment

RSS feed for comments on this post · TrackBack URL

Hot 100 osCommerce Shops

View the osCommerce HOT 100These are the best looking, most exceptional osCommerce Stores as voted for by you.

New to osCommerce - get inspiration from these beautiful shops. Reckon your site has what it takes to become a member of the HOT 100? Submit it!