Freight template of e-commerce system design (2)


Freight template of e-commerce system design (2)

E-commerce people use it every day, such as a cat, a dog, etc.
The design of e-commerce system seems complex and simple, and it seems simple and complex
This chapter is suitable for junior and intermediate engineers to have a close look. Please feel free to do so


In the order system, the freight template is an important part of the order system. It seems to be a simple setting. In its design, there are still many problems to consider. In the last chapter, we discussed some rules of freight and how to design in the database table. This chapter talks about how to calculate the freight


Through the data table we established in the previous article, which freight template is bound to the commodity

$templateId = Product::where('id',$product)->value('template_id');
if($template == 0) return [];

Multiple rules may be used for specified items, such as this
Freight template of e-commerce system design (2)

  • Is it free
  • Freight in designated area
  • How much is the freight or parcel post under certain conditions

With so many conditions, we need to ensure that all rules can be retrieved, and the calculation speed must be improved.

Before calculating, you should think about several possibilities and choose the priority, just like there are

Three different colors of the ball, there are many ways to piece together

I remember it should be a primary school math problem.

Principle of proximity

We must have done this interview question

Sort a scrambled string

That’s in the classic way

  • Quick sort
  • Select sort
  • Insert sort
  • Bubble sort

Of course, we don’t talk about sorting. We want to think about how to query the information we want quickly through this kind of algorithm.

The core of sorting algorithm merging is through comparison. Whether the calculation starts from the middle, or from the beginning or from the end, it is through guessing the presentation of the string itself.

Then, where should we start “sorting” for freight calculation?

According to the business, first of all, we consider the three rules about whether to send the goods by parcel, the freight in the designated area, and the fulfillment of the specified conditions. Most of them have such priority.

To reach the specified conditions > to specify the area of freight > whether to include mail

1、 AchievedSpecify conditionsNot in the calculation of the specified regional conditions and whether the package
2、 ReachedSpecified regional conditionsYou don’t know if it’s postal

Is the idea a little clearer? Let’s go to the code

actual combat

The demo is pseudo code, and this kind of calculation can’t be handed over toFront end computingThere will be many security problems, such as data tampering, etc. lazy back-end is not a good back-end

When we get the templateid, in the table product_ template_ Query the associated rules in config. This is a one to many data column, which means that multiple items will be queried. First, we will query the specified conditions.

select count(1) product_ template_ config where template_ Id = $templateid and is there a specified condition

When there are specified conditions, the calculation is performed, such as

  • Reach the specified amount, the freight is fixed
  • How much is the freight after reaching the specified amount

The calculation is simple. I learned it in primary school

Commodity quantity * commodity unit price > specified amount = freight

If there is no specified condition, you can query the freight rate of the specified city. For the city, we use JSON storage, and there will be multiple items, which can’t be avoided. In this way, you will say that many cities will not lead to low efficiency?

Do any function withActual businessIn addition to Tibet, Xinjiang and remote areas, there will be businesses in one city setting different freight rates. Then we will use two methods

The first one: the dead circle method

$list = "select * from `product_template_config` where template_id = $templateId";
$city = [];
   If ($list - > City = = Beijing){
        return price;

The second is fuzzy query

select * from `product_ template_ Config ` where city like "% Beijing%"

Like sometimes leads to index invalidation, so we should pay special attention to it

Finally, if the above two rules are not met, we will go back to the simplest way to customize the freight and whether to include mail. If you define the freight, you will calculate the final freight. If you want to include mail, you can directly return 0 and it will be finished.

The flow chart is as follows

Freight template of e-commerce system design (2)

Some thoughts

At this point, our freight template design and actual combat is over.

Of course, this is only a design idea. Using this method can not withstand the impact of access. I just provide ideas.

Finally, I’ll give you some thoughts. You may as well practice it.

1. The priority of retrieval is set in advance. We can analyze the setting habits of a certain merchant regularly every day. For example, the merchant often sets the package mail, so we can change the location of priority calculation.

2. For applications with too much concurrency, try redis? The only change is the data structure

3. Is there any more practice to calculate the freight according to the shopping habits of users?

4. Consider which process should be included in if there are free or preferential packages?

Any simple function, in the application of continuous iteration, will become no longer simple. Look forward to your best practices.


Thank you for your attention, I hope this article can help you, thank you.