Two common mistakes when coding a website yourself … sales

As a programmer, everyone should know the concept … web sales. Code a sales website is a great way to apply new language / technology . Through product registration, login, and show functions, we learn how to decentralize, routing, paging, processing business logic.

Many students think that web sales code is a simple affair , much of it is just adding and removing edits. Really? Read this article to see if you make two mistakes below .

Mistake 1 – Do not save the price of the product on the invoice

The relationship between Order and Item is a many-to-many, so we have to add a table in the middle to connect these two tables. In theory, when displaying the invoice, can refer to the Item table to get the price of the product and display it.

However, in fact, the price of products often changes . Suppose 10/5, the price of a loaf of bread is 10k; by 12/5, the price of a loaf of bread is 15k. When we view the bill again on May 10, we see that the price of the bread is still 15k , because it refers to the current price in the Item table. In addition, the price is affected by promotions and discounts. If only the product price is saved, when the report is displayed or exported, the information will be misleading.

So how do we handle it? The easiest way is to add 1 column containing the invoice price in the OrderItem table. Sometimes a separate table is used to save price changes for each product, then based on the date on the invoice to find the price of the product on that day.

Database after editing

(Note: OrderItem table can use ItemID and OrderID as the primary key key, I create ID for easy query when using Entity Framework only)

Mistake 2 – Save the shopping cart in the session

When learning about sessions in the Web, people often use web sales as an example. Each user has a separate session, so we use the session to save the shopping cart for each user. However, this method is only suitable for web demo or project in school, but in reality … it is very easy to generate problems.

We often do not understand the buying process of users. In many cases, users leave the item in the basket and check out. Sometimes, users leave the goods in the basket to save temporarily, accumulate a few days for many and then buy 1 turn to enjoy the promotion. If you save the shopping cart in the session, the cart will disappear when the session timeout, causing discomfort to the user.

Or suppose users use both web and mobile to buy goods, if stored on the session, when switching to another device, users will not see their shopping cart anymore! Also, in the technical aspect, the session running in the server farm environment will encounter some problems ( see also ).

So how do we handle it?

Method 1 – Save in local storage : This method cannot be used with the old browser (may use a temporary cookie). This way lightens the server load because data is read and written on the client side. The downside is that when the device is changed, the shopping cart cannot be viewed. The side seems to be using this method.

Method 2 – Save in database: With an existing user, we save their cart in DB. With a new user not logged in, we save an id string in their cookie or local storage, use that cookie to query the shopping cart on the DB.

If the user only gives the item to the basket, then does not return to the website, the DB will have a lot of junk data. To solve this problem, people often create several tasks that run in the background to delete garbage data from DB. (On Amazon shopping cart deleted after 90 days). In addition, to increase the speed of saving, we can use some NoSQL database with fast reading and writing speed to save the cart.

Nopcommerce , one of C # famous e-commerce frameworks also saves the cart to the database (If you have PHP code, please confirm whether the framework you use has saved the cart to DB, I think yes).

If you still don't believe what you say, try shopping on Amazon and Ebay. You will see, after you put the item in the basket, you log out, use the phone to enter, the cart is still there. This can only be done when the cart is stored in the database.


Well, don't underestimate the complexity of a sales website. In order to make a handy sales site, it is necessary to have an understanding of UI / UX (see more 1 button article for $ 300 million ). Some complicated functions such as product buyer A also buy product B, recommend products according to user characteristics, … also requires some algorithms data mining is very complex behind.

In general, self-coding a sales website is a great way to learn . But if the code for the business needs to be taken seriously, you should use some of the Framework / CMS available. In Vietnam, Web sales are quite cheap because people use the framework and then re-customize it quickly. If the design and code from the beginning, add a number of complex functions, it does not cost 3-5 million .

ITZone via toidicodedao

Share the news now