Progressive quantity based shipping costs with a max cost in Woocommerce Progressive quantity based shipping costs with a max cost in Woocommerce wordpress wordpress

Progressive quantity based shipping costs with a max cost in Woocommerce


You can't use available settings to charge a variable shipping cost as defined:

  • 3,50 for the first item
  • 4,50 for more than one item (2 or more)

So you will need to use the following custom hooked function:

add_filter('woocommerce_package_rates', 'custom_shipping_costs', 10, 2 );function custom_shipping_costs( $rates, $package ){    // Get cart items count    $items_count = WC()->cart->get_cart_contents_count();    // Your cost settings    $new_cost = $items_count > 1 ? 4.5 : 3.5;    // Loop through shipping methods rates    foreach ( $rates as $rate_key => $rate ){        $has_taxes = false;        // Targeting "Flat rate" shipping methods        if ( 'flat_rate' === $rate->method_id ) {            $default_cost = $rates[$rate_key]->cost;            $rates[$rate_key]->cost = $new_cost;            $rate_conversion = $new_cost / $default_cost;            // Taxes rate cost (if enabled)            foreach ($rates[$rate_key]->taxes as $key => $tax){                if( $tax > 0 ){                    $taxes[$key] = $tax * $rate_conversion;                    $has_taxes = true;                }            }            if( $has_taxes )                $rates[$rate_key]->taxes = $taxes;        }    }    return $rates;}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Once the code is saved, go in your flat rate settings and set the rate cost to 1.00:

enter image description here

Now the shipping rate will change from 3.50 (for one item) to 4.50 (for more than one items)