在订购过程中添加折扣的最佳方式是什么?

发布于 2024-10-31 01:35:23 字数 272 浏览 5 评论 0原文

假设一个网站销售产品并希望允许使用优惠券代码进行折扣。构建这个结构的最佳方法是什么?这就是我的想法,你能确认这是正确的方法吗?请问还有其他建议吗?别人是怎么做到的?

我的想法是这样的:

订单中将包含给定的产品作为订单项目。将输入优惠券代码,如果输入正确的代码,则会将具有负值的新订单项添加到订单中。换句话说,现在订单将包含两项:

  • 产品 - 10 美元
  • 折扣 - 5 美元 总计:5 美元

这是获得折扣和优惠券的正确方式吗?

Lets say a website sells products and wants to allow discounts using coupon codes. What is the best way to structure this? Here's what I have in mind, can you confirm that this is a right way to go about it? Any other suggestions please? How do other's do it?

Here's my thinking:

A order will have a given product in it as an order item. There will be a enter coupon code input, and if correct code is entered a new line item with negative value will be added to the order. In other words now order will consist of two items:

  • Product - $10
  • Discount -$5
    Totoal: $5

Is this the right way to approach discounts and coupons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖风昔人 2024-11-07 01:35:23

有多种方法可以做到这一点。没有一种解决方案。然而,这里有一些陷阱

  • 用户从购物车中删除折扣商品?
  • 用户添加多个相同的优惠券代码?
  • 用户有一个空购物车?

最后,在订单完成时应用折扣的解决方案最不容易出现上述情况。不要尝试跟踪订单并在数据库或会话中添加标志。这将是徒劳的。您确实需要重点测试优惠券的条件是否得到满足。设想像 applyCoupons()eligibleForDiscount($coupon) 这样的函数。

There are multiple ways to do this. There's no one solution. However, here are a few gotchas.

  • User removes discount item from the cart?
  • User adds multiple of the same coupon codes?
  • User has an empty cart?

In the end, a solution that applies the discount upon order completion is going to be least prone to the above. Don't try to track the order and add flags in the database or session. It's going to be futile. You really want to focus on testing if the conditions of the coupons have been met. Envision functions like applyCoupons() or eligibleForDiscount($coupon).

酒儿 2024-11-07 01:35:23

您的数据库中应该有两个关系表。
优惠券有不同的折扣值吗?

您可能有包含 ID、代码、折扣的 coupons 表,并且您可以在 code 字段中存储用逗号分隔的优惠券代码,如下所示(ABC、BBC、CCS)或每个线。

如果用户在输入字段中输入优惠券 ID,请在数据库中检查它,读取折扣并执行 $final_price = $price * $discount (假设您将折扣存储为 0.2(20%) 如果您想要逗号分隔

的优惠券代码,请执行以下操作:

$discount = 1;
while ( $var = mysql_fetch_assoc($result)) {
   $coupon = explode(',', $var['code']);
   $count = count($coupon);
   for ($i = 0; $i <= $count; $i++) {
     if($_POST['input_coupon'] == $coupon[$i]) { $discount = $var['discount'] }
   }

然后您 $final_price = $price * $discount ;
您可以将其放入 AJAX POST 请求中,瞧;

或者每个优惠券代码都使用一行,这样更简单。

或者执行 JOIN 查询(​​通过 id 链接表)来简化过程。

You should have two relational tables in data base.
Coupons have different discount values ?

You might have coupons table with id, code, discount and you could store coupons code separated by comma in the code field like this (ABC, BBC, CCS) or one per line.

if user enter coupon id in the input field , check it in the database , read the discount and do $final_price = $price * $discount (assuming you'll store discount as 0.2 (for 20%) and so on .

If you want comma separated coupon codes do this to manipulate them :

$discount = 1;
while ( $var = mysql_fetch_assoc($result)) {
   $coupon = explode(',', $var['code']);
   $count = count($coupon);
   for ($i = 0; $i <= $count; $i++) {
     if($_POST['input_coupon'] == $coupon[$i]) { $discount = $var['discount'] }
   }

and then you $final_price = $price * $discount ;
You can put it in an AJAX POST request and voila;

Or use one line with every coupon code and is more simple .

Or do a JOIN query (tables linked by id) to simplify the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文