Trying to implement a promotion code box in php for an online store
I'm trying to make a promo code box in php such that I can give people different codes for specific discounts on items in my online store. The basic idea is to have them input the code into the box at time of purchase for a coupon-like effect.
I've been searching around and haven't been able to find anything regarding th开发者_运维技巧is or something of a similar nature. Any help would be greatly appreciated, thanks!
Here are some thoughts/ideas of my own that I used to create a coupon-like system. This should help to get you started in designing a backend.
- You want the (let's call it 'Coupon') to have the ability to be tied to specific items or not, as well as have the ability to be tied to Categories of items (multiple items).
- The Coupon should be able to define either a set amount off or a set percentage off either a portion of the sub-total or the entire sub-total.
- Tax should be calculated based off of pre-discount sub-total. You can choose to do it the other way, but this is a decision point to consider.
- You should be able to specify a start date and end date for the coupon.
- You should have the ability to track coupon usage by item and by user. If you have abstract groups of users, then you should be able to track by user group as well.
- You should be able to require other items in the coupon which are not discounted in order to get a discount on other items. (Buy
This
GetThat
Half Off types of deals) - You should be able to define a minimum number of Items until a coupon can take effect (Buy 3 Tires Get the 4th Free!), as well as a maximum number of times this can be applied (You may not want: Buy 30 Tires Get 10 Tires Free!)
- You can decide to restrict certain coupons to a certain user or group of users. You would need to find a way to enforce this restriction if you do.
Just reading this leads me to see at least 3 tables:
Coupons
, Item_Coupons
, User_Coupons
Where Item_Coupons
and User_Coupons
map your tables for Items and Users to Coupons. You can also include other tables that may make things easier, such as Coupons_RequiredItems
which is another map from Coupons to items that defines the required items for the coupon. The structure of these tables depends heavily on precisely how you intend to implement this.
Of course, if your only intention is to provide X% off an individual Item, then following this would be way too complicated, but much more flexible.
The bad way:
switch($_GET['code']) {
case "foo":
$price -= $price * 0.1;
break;
case "bar":
$price -= $price * 0.25;
break;
case "baz":
$price -= $price * .33;
break;
default:
break;
}
Better would be to have the mapping code -> discount stored somewhere in a database and read it from there.
精彩评论