php - 创建折扣代码系统 (MySQL/php)

标签 php mysql

我们有一种产品,通过 PayPal 付款。在去 PayPal 之前需要申请折扣。我们想创建一个系统,人们可以输入礼券代码以免费获得产品(即 100% 折扣)或输入一些代码以获得特定折扣(即 SAVE10 - 获得 10% 折扣)。

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

打算用MySQL和php拼起来。

有没有人已经这样做并将某些东西放在一起了?或者知道我们可以用来节省时间的一些代码?不需要整个购物车只需要折扣代码部分..

谢谢。

最佳答案

这实际上是一种单类功能。你需要一个看起来像这样的类接口(interface)

 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
}

数据库表可能是这样的

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语句:

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

然后只需在验证结帐表单时使用此类。例如,

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

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

好吧,这就是我会做的。

关于php - 创建折扣代码系统 (MySQL/php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7278925/

相关文章:

mysql - 如何在 MySQL 中返回数据透视表输出?

java - 如何对 JOptionPane.showMessageDialog 的 OK 执行操作

php - 区分不同版本的网站

php - 在 codeigniter 中使用 super 对象有什么好处

php - 将输入字符串形成 float

mysql - Laravel orderBy 大大减慢了响应速度

mysql - 对多个表中的多个计数求和

javascript - 使用 MySQL 中的数据自动完成 <ol> 和 <li>

php - 如何在 FPDF 中将文本居中?

用于选择列的总和和计数的php代码