我们有一个产品,通过PayPal支付。在去贝宝之前需要申请折扣。我们想创建一个系统,人们可以输入礼券代码获得免费产品(即100%折扣)或输入一些代码获得特定的折扣(即SAVE10 -获得10%的折扣)。
有些代码只能使用一次(如礼券),有些代码可以多次使用(如SAVE10)。有些还会有有效期。
将MySQL和php放在一起。
有没有人已经这样做了,并把一些东西放在一起?或者知道一些可以节省时间的代码?不需要整个购物车只需要折扣码部分..
谢谢。
这实际上是一个类的功能。您需要一个像这样的类接口
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();
}
}
我就是这么做的
"不需要整个购物车,只需要折扣代码部分…"
这是一个非常宽泛的问题,没有具体的答案。完成您所谈论的需要大量的集成,这些集成将具体到您正在进行的工作。如果您将代码发布到Github或其他地方并且遇到特定问题,请尝试重新发布您遇到的任何困难。
如果您只是在寻找一些这样做的例子,请查看http://www.oscommerce.com/community/contributions,4269,看看阅读源代码是否会给您一些实现的想法。