创建折扣代码系统(MySQL/php) [英] Creating Discount Code System (MySQL/php)

查看:84
本文介绍了创建折扣代码系统(MySQL/php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一种产品,付款是通过PayPal进行的.前往贝宝(PayPal)之前,请先申请折扣.我们希望创建一个系统,使人们可以输入礼品券代码以获取免费产品(即100%折扣),或输入一些代码以获取特定折扣(即SAVE10-享受10%折扣).

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

有些代码只能用于一种用途(即礼券),有些可以多次使用-即SAVE10.有些还会有有效期.

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.

要使用MySQL和php组合在一起.

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

谢谢.

推荐答案

这实际上是一种功能.您需要一个看起来像这样的类接口

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


注意::验证过程可能只是一个简单的SELECT语句:


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();
   }
}

好吧,那是我会做的.

这篇关于创建折扣代码系统(MySQL/php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆