2011-09-02 63 views
7

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

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

要使用MySQL和PHP放在一起。

有没有人在那里已经做了这件事,把东西放在一起?或知道我们可以用来节省时间的一些代码?不需要整个购物车只是折扣代码部分..

谢谢。

+0

为什么java标签? –

+0

删除..是thining也许一些Java家伙有一个想法..但是,决定删除..对不起.. – user718359

回答

13

这实质上是一类功能。你需要一个类的界面,看起来像这样

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 
} 

db表看起来是这样的

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

那么,这有点如何我会这样做。

+0

看起来不错..你有没有把它放在行动的任何地方,我可以看到在行动? – user718359

+0

从来没有得到合同这样做,没有。但是任何其他解决方案都只是矫枉过正。其余的仅仅是验证(从数据库获取数据,检查值等) –

+0

嗯..这看起来不错,现在会尝试实施..你在线的下一个小时左右,所以我可以让你更新? – user718359

0

“不需要整个购物车刚折扣代码部分。”

这是一个没有具体的答案一个非常宽泛的问题。完成你正在谈论的内容需要大量的整合,这些整合特定于你正在处理的内容。如果您将代码发布到Github或某处并遇到特定问题,请尝试重新发布您遇到的任何问题。

如果你只是想找一些这样做的例子,看看http://www.oscommerce.com/community/contributions,4269,看看是否阅读源代码给你一些实现的想法。

+0

谢谢看了。但不想使用oscommerce购物车等..有点矫枉过正..希望有一个快速简单的解决方案.. – user718359