## How to calculate product shipping cost depending on product & weight

I have the following conditions in my store.

2 type of products, but in the future could be more:

• Product A: 0.594 KG.
• Product B: 1.122 KG.

Shipping packages of 2KG max., every time that an order reaches 2KG of weight I need to add a new package. For example:

• One product B = 1.122KG
• Two product A = 0.594KG*2 = 1.188

I will need to packages.

Different prices for the weight of each package (maximum 2KG, as stated before):

• 0 to 0.1 (kg) – 1,37€
• 0,1 to 0.2 (kg) – 2,56€
• 0,2 to 0.3 (kg) – 4,33€
• 0.3 to 0.4 (kg) – 5,06€
• 0.4 to 0.5 (kg) – 5,65€
• 0.5 to 0.75 (kg) – 8,21€
• 0.75 to 1 (kg) – 9,01€
• 1 to 1.5 (kg) – 14.14€
• 1.5 to 2 (kg) – 16.30€

So if somebody makes an order of one product B (1.122KG) and two product A (0.594KG*2) it needs to be distributed as follows:

In one package a product B and a product A: 0.594+1.122 = 1.716; – 16.30€
In the other package a product B – 8,21€
End price: 24,51€.

So far I have the following code which does not address the problem very well because it doesn’t split the weight into products:

``````// Gastos personalizados
``````

function custom_shipping_costs( \$cost, \$package, \$shipping_rate_id, \$class ) {

``````\$weight = WC()->cart->get_cart_contents_weight();

\$complete_packages = floor(\$weight/2);
\$no_complete_packages = fmod(\$weight, 2);

if ( ! in_array( \$shipping_rate_id, \$add_to_rate_ids ) ) {
return \$cost;
}

if ( \$no_complete_packages > 0 && \$no_complete_packages <= 0.1 ) {
} elseif ( \$no_complete_packages > 0.1 && \$no_complete_packages <= 0.2 ) {
} elseif ( \$no_complete_packages > 0.2 && \$no_complete_packages <= 0.3 ) {
} elseif ( \$no_complete_packages > 0.3 && \$no_complete_packages <= 0.4 ) {
} elseif ( \$no_complete_packages > 0.4 && \$no_complete_packages <= 0.5 ) {
} elseif ( \$no_complete_packages > 0.5 && \$no_complete_packages <= 0.75 ) {