问题
Currently, I'm looking for best practice to "applying discounts" to cart/order. So, I'm planning to implement such kind of discounts as...
fixed user's discount (for example, I'd like to give 10% discount to my favourite customer)
discount for number of items (for example, you're buying 10 different colored pens and you'll be getting discount of 1.5%)
discount for coupon (for example, during promo action we've produced 100 coupons with 10% discount each. Coupons work only for one order and expire on yyyy-mm-dd)
present for purchasing item or group of items (for example, you're buying pen, list of paper and shop presents a sharpener to you)
discount for total order price (for example, you're buying 10 pens and gettin no discount, then you're adding 5 more pens and getting 5% discount)
Only one discount could be applied to specific item. We're always applying biggest discount. The most profitable discount for user.
Beside that admin should be able to modify items price in specific order and cancel discounts in this order.
Frankly speaking, it is my first ecommerce application and it seems to me pretty hard task to implement all these kinds of disounts.
Ok, now let me tell you how I'm going to implement...
fixed user's discount It is just a field in users table which contains information about discount. It is being set manually through control panel or automatically by cron for uses having total order summ over that N.
discount for number of items I'm going to create additional table storing items' groups of such discounts. Also I'll need one more table to connect this groups with items in shop.
discount for coupon Just a table with coupons containing field with expire day, status of coupon, coupon discount and maybe user_id (owner of coupon). Probably I will also implement coupons for categories of items.
present for purchasing item or group of items It is real pain in the ass and I totally don't know how implement. Please help!
discount for total order price Pretty easy, imho.
Ok, now I looking for best practices of storing this kinds of discounts in DB. Also I'm looking for OOP practices to applying this discounts to Cart. Any help is appreciated!
PS: Sorry for such long post, but I think that it will be interesting not only for me.
Thank you.
回答1:
A basic solution for the present for purchasing item or group of items requirement is to have something like a bundle and a bundleItem table.
bundle table.
id int primary key
giftproductId int foreign key - product table
bundleitem table.
id int primary key
bundleid int foreign key - bundle table
requiredproductid int foreign key - product table
Your bundle table contains the gift item id and the bundleitem table holds all the items associated to the bundle which the user must order to qualify for the gift.
Your 'Bundle' class would contain an array of these required product items, the gift item and a method which takes a shopping cart object and checks to see whether the cart contains every required product for this particular bundle offer - something like a simple foreach within a foreach loop...
This is a simple solution things can get complex depending on the rules for these gift items e.g. if I order 2 pens and 2 lists of paper do I get 2 sharpeners? - Good luck with it!
来源:https://stackoverflow.com/questions/5825619/shopping-cart-and-different-kinds-of-discounts-storing-discounts-in-db-and-appl