Answer the question
In order to leave comments, you need to log in
How to store product price and discount?
Let's imagine a situation where on the website, when adding a new product in the admin panel, you can specify the price of the product and the size of the discount as a percentage, or the price of the product already at a discount.
Which option would you prefer for storing in a DB? From the point of view of correctness and reducing problems in the future (such as sorting by product value).
Options:
1. price (1000)
price_discounted (900)
2. price (1000)
discount_percent (10)
3. price (1000)
discount_sum (100)
Answer the question
In order to leave comments, you need to log in
No one.
Store only the base price.
For flexible and efficient management of discounts, promotions, personal discounts, you need a separate table, which in the future will allow you to create separate rules: summation of discounts, cumulative discounts, basket only discounts, discounts only in the catalog, or for a specific group of customers with restrictions on dates, delivery conditions and so on and so forth.
A plate with data about the product, and a separate plate with discount fields for the product with the start and end dates of the discount. At the level of logic, you need to make a validator when creating, whether there is a discount for this product in the created time interval, if there is, throw 400, otherwise create. An update, if necessary, can be done using the ID of the discount. Deleting discounts is best done through soft delete
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question