2011-09-02 87 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