【问题标题】:Creating Discount Code System (MySQL/php)创建折扣码系统 (MySQL/php)
【发布时间】:2011-11-08 21:15:25
【问题描述】:

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

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

打算用mysql和php来拼凑。

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

谢谢。

【问题讨论】:

  • 已删除.. 可能有些 Java 人员有想法.. 但是是的,决定删除.. 抱歉..

标签: php mysql


【解决方案1】:

这本质上是一类功能,真的。你需要一个看起来像这样的类接口

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

嗯,这就是我的做法。

【讨论】:

  • 看起来不错。。你有没有把它付诸行动,我可以看到在行动的任何地方?
  • 从来没有签订过这样做的合同,没有。但是任何其他解决方案都只是矫枉过正。剩下的只是验证(从数据库中获取数据,检查值等)
  • 嗯.. 这看起来很棒,现在将尝试实施.. 你在接下来的一个小时左右在线,我可以让你更新吗?
  • 想要一个展示/隐藏框..有折扣吗?如果他们点击代码框出现..按提交..数据库检查等..如果代码正确将显示新的折扣金额并应用味精折扣..否则折扣不适用..然后如果一切正常,他们按下按钮去到贝宝..
  • 是的,看起来不错。您甚至可以通过 Ajax 调用执行折扣并返回更新后的价格
【解决方案2】:

“不需要整个购物车,只需要折扣代码部分..”

这是一个非常广泛的问题,没有具体的答案。完成您正在谈论的内容需要大量集成,这些集成将特定于您正在从事的工作。如果您将代码发布到 Github 或其他地方并遇到特定问题,请尝试重新发布您遇到的任何困难。

如果您只是在寻找一些这样的例子,请查看http://www.oscommerce.com/community/contributions,4269,看看阅读源代码是否会给您一些实现思路。

【讨论】:

  • 谢谢看看。但不想使用oscommerce购物车等。有点矫枉过正..希望有一个快速简单的解决方案..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-09
  • 1970-01-01
  • 2013-04-28
  • 2018-01-07
  • 1970-01-01
相关资源
最近更新 更多