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);

$add_to_rate_ids = array( '471' );

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

$add_cost_no_complete_packages = 0;
    if ( $no_complete_packages > 0 && $no_complete_packages <= 0.1 ) {
        $add_cost_no_complete_packages = 1.37;
    } elseif ( $no_complete_packages > 0.1 && $no_complete_packages <= 0.2 ) {
        $add_cost_no_complete_packages = 2.56;
    } elseif ( $no_complete_packages > 0.2 && $no_complete_packages <= 0.3 ) {
        $add_cost_no_complete_packages = 4.33;
    } elseif ( $no_complete_packages > 0.3 && $no_complete_packages <= 0.4 ) {
        $add_cost_no_complete_packages = 5.06;
    } elseif ( $no_complete_packages > 0.4 && $no_complete_packages <= 0.5 ) {
        $add_cost_no_complete_packages = 5.65;
    } elseif ( $no_complete_packages > 0.5 && $no_complete_packages <= 0.75 ) {
        $add_cost_no_complete_packages = 8.21;
    } elseif ( $no_complete_packages > 0.75 && $no_complete_packages <= 1 ) {
        $add_cost_no_complete_packages = 9.01;
    } elseif ( $no_complete_packages > 1 && $no_complete_packages <= 1.5 ) {
        $add_cost_no_complete_packages = 14.14;
    } elseif ( $no_complete_packages > 1.5 && $no_complete_packages <= 2 ) {
        $add_cost_no_complete_packages = 16.30;
    }

return ( $cost + $complete_packages * 16.30 + $add_cost_no_complete_packages - 0.01 );

 }
  add_filter( 'was_calculate_shipping_costs', 'custom_shipping_costs', 10, 4 );

Read more here: How to calculate product shipping cost depending on product & weight

Leave a Reply

Your email address will not be published. Required fields are marked *