创建折扣代码系统 (MySQL/php)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7278925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating Discount Code System (MySQL/php)
提问by user718359
We have one product, payment is by PayPal. Before going to PayPal need to apply a discount. We want to create a system where people can enter a gift certificate code to get product free (ie 100% discount) or enter some code to get a specific discount (ie SAVE10 - get a 10% discount).
我们有一种产品,通过 PayPal 付款。在去 PayPal 之前需要申请折扣。我们想要创建一个系统,人们可以在其中输入礼券代码以免费获得产品(即 100% 折扣)或输入一些代码以获得特定折扣(即 SAVE10 - 获得 10% 折扣)。
Some codes will be just for one use (ie gift certificates), some can be used multiple times - ie SAVE10. Some will also have expiry dates.
有些代码只能使用一次(即礼券),有些可以多次使用——即 SAVE10。有些还会有到期日。
Going to use MySQL and php to put together.
打算用 MySQL 和 php 组合起来。
Has anyone out there already done this and put something together? or know of some code we can use to save time? Do not need entire shopping cart just the discount code parts..
有没有人已经做过这个并将一些东西放在一起?或者知道一些我们可以用来节省时间的代码?不需要整个购物车只需要折扣代码部分..
Thank you.
谢谢你。
回答by Yanick Rochon
This is essentially a one class functionality, really. You'd need a class interface that would look like this
这实际上是一类功能。你需要一个看起来像这样的类接口
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
}
The DB table could look like this
数据库表可能如下所示
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
Note :The validation process could just be a simple SELECT
statement :
注意:验证过程可能只是一个简单的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())
Then just use this class when validating checkout form. For example,
然后在验证结帐表单时使用这个类。例如,
if (!empty($discountCode)) {
$discount = ProductDiscount::validate($discountCode);
if ($discount !== null) {
$price *= (1 - $discount->getDiscount());
$discount->consume();
}
}
Well, that's somewhat how I would do it.
嗯,这就是我会怎么做。
回答by seanp2k
"Do not need entire shopping cart just the discount code parts.."
“不需要整个购物车只需要折扣代码部分..”
This is a really broad question with no specific answer. Accomplishing what you're talking about requires a lot of integration that would be specific to exactly what you're working on. If you publish your code to Github or somewhere and are having a specific problem, try re-posting any difficulties you run into.
这是一个非常广泛的问题,没有具体的答案。完成您所谈论的内容需要大量的集成,这些集成将特定于您正在从事的工作。如果您将代码发布到 Github 或其他地方并遇到特定问题,请尝试重新发布您遇到的任何困难。
If you're just looking for some examples of this being done, check out http://www.oscommerce.com/community/contributions,4269and see if reading the source code gives you some implementation ideas.
如果您只是在寻找完成此操作的一些示例,请查看http://www.oscommerce.com/community/contributions,4269,看看阅读源代码是否为您提供了一些实现思路。