What is the best way to add discounts to ordering process?
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 m开发者_如何学JAVAind, 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?
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)
.
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.
精彩评论