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

7
我们有一个产品,使用PayPal进行付款。在前往PayPal之前需要申请折扣。我们希望创建一个系统,让人们可以输入礼品券代码以免费获得产品(即100%折扣),或输入一些代码以获得特定折扣(即SAVE10-获得10%的折扣)。
有些代码只能使用一次(即礼品券),有些可以多次使用-比如SAVE10。一些代码还会设置到期日期。
我们打算使用MySQL和PHP来组合。
有没有人已经做过这个并且编写了相关代码?或者知道我们可以使用的代码以节省时间?不需要整个购物车,只需要折扣码部分即可。
谢谢。

已删除..我在想也许一些Java的人有什么想法..但是是的,决定删除..抱歉.. - user718359
2个回答

14

实际上,这基本上是一个一类功能。你需要一个类接口,它看起来像这样

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

好的,那就是我会做的方式。


看起来不错.. 你把这个实施到哪里了吗?我能在哪里看到它的实际效果? - user718359
还没有得到执行合同的机会,但是其他任何解决方案都会过度设计。其余部分只是简单的验证(从数据库获取数据,检查值等)。 - Yanick Rochon
是的,看起来不错。您甚至可以通过Ajax调用执行折扣并返回更新后的价格。 - Yanick Rochon
另外,如果他们输入代码..按提交按钮..代码检查..但然后不去PayPal支付,在稍后回来并输入代码..(如果只使用1个代码)..代码将无法工作?我们需要在PayPal使用后再添加一些其他内容吗? - user718359
让我们在聊天中继续这个讨论。点击此处进入聊天室 - Yanick Rochon
显示剩余5条评论

0
不需要整个购物车,只需要折扣代码部分。。
这是一个非常广泛的问题,没有具体的答案。实现您所说的需要大量的集成,这将取决于您正在处理的确切内容。如果您将您的代码发布到Github或其他地方,并且遇到特定的问题,请尝试重新发布您遇到的任何困难。
如果您只是想找一些已经完成的示例,请查看http://www.oscommerce.com/community/contributions,4269,看看阅读源代码是否给您一些实施思路。

谢谢您的建议。但我不想使用oscommerce购物车等等,这有点过头了。希望能找到一个快速简单的解决方案。 - user718359

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接