Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Discount Code System (MySQL/php)

Tags:

php

mysql

We have one product, payment is by PayPal. Before going to PayPal need to apply a discount. We want to create a system where people can enter a gift certificate code to get product free (ie 100% discount) or enter some code to get a specific discount (ie SAVE10 - get a 10% discount).

Some codes will be just for one use (ie gift certificates), some can be used multiple times - ie SAVE10. Some will also have expiry dates.

Going to use MySQL and php to put together.

Has anyone out there already done this and put something together? or know of some code we can use to save time? Do not need entire shopping cart just the discount code parts..

Thank you.

like image 548
user718359 Avatar asked Sep 02 '11 03:09

user718359


People also ask

How can I add promo code in PHP?

php", type: "POST", data: {code: $("input#promocode"). val(), id:<? php echo $result['id']; ?>}, success: function(data) { if (data == 'FALSE') { $("span#wrong_code").

Is there a way to find discount codes?

Use a trusted source. The better path is to check websites such as RetailMeNot, DealsPlus, Coupon Cabin and Slickdeals. These sites work with thousands of retailers and brands, as well as user submissions, to aggregate sales and codes. At RetailMeNot, all codes are tested and verified before being published.


1 Answers

This is essentially a one class functionality, really. You'd need a class interface that would look like this

 class ProductDiscount {
   /**
    * Create a NEW discount code and return the instance of
    *
    * @param $code string      the discount code
    * @param $discount float   price adjustment in % (ex:        
    * @param $options array    (optional) an array of options :
    *                            'expire'   => timestamp    (optional)
    *                            'limited'  => int          (optional)
    * @return ProductDiscount                         
    */
   static public function create($code, $discount, $options = NULL);

   /**
    * This essentially validate the code, and return the instance of the
    * discount if the code exists. The method returns null if the discount 
    * is not valid for any reason. If an instance is returned, to apply
    * the discount, one should invoke the "consume()" method of the instance.
    *
    * @param $code string
    *
    * @return ProductDiscount|null
    */
   static public function validate($code);

   private $_code;     // string
   private $_discount; // float
   private $_expire;   // unix timestamp (see time()) or null if unlimited
   private $_count;    // int or null if unlimited

   private function __construct();
   public function getCode();      // return string
   public function getDiscount();  // return float
   public function isLimited();    // return bool; true if $_count || $_expire is not null
   public function getCount();     // returns int; how many of this discount is left or null if unlimited
   public function getExpireDate();// returns int (unix timestamp) or null if unlimited

   public function consume();      // apply this discount now

   public function consumeAll();   // invalidate this discount for future use
}

The DB table could look like this

id UNSIGNED INT(10) NOT NULL AUTOINCREMENT  -- (Primary Key)
code VARCHAR(12) NOT NULL                   -- (Indexed, unique)
discount UNSIGNED INT(3) NOT NULL           -- percent value 0..100
expire DATETIME DEFAULT NULL                -- unlimited by default
count INT(10) DEFAULT 1                     -- can be NULL

Note : The validation process could just be a simple SELECT statement :

SELECT * 
  FROM tblproductdiscount
 WHERE code = {$code}                  -- $code = mysql_real_escape_string($code)
   AND (count IS NULL OR count > 0)
   AND (expire IS NULL or expire > NOW())

Then just use this class when validating checkout form. For example,

if (!empty($discountCode)) {
   $discount = ProductDiscount::validate($discountCode);

   if ($discount !== null) {
      $price *= (1 - $discount->getDiscount());
      $discount->consume();
   }
}

Well, that's somewhat how I would do it.

like image 93
Yanick Rochon Avatar answered Sep 23 '22 19:09

Yanick Rochon