I'm developing a virtual store and I need to implement the freight calculation for the purchase, I managed to make this implementation but the problem is that with a single product in the cart I get the correct return, but with several products in the cart, the values do not beat with the logistics company simulator.

I wanted to know how I can calculate the freight of several products together. If I calculate the cube of each one separately and then I add all, or if I calculate the cube with the sum of the dimensions of the products, or what is the correct way to do it.

I am lost with regard to this calculation because not on the internet there is only content explaining about the calculation of individual products and I can not solve this question.