Changing Woocommerce flat rate every nth number of items

To change “Flat rate” shipping cost based on cart items quantity count that belongs to a specific shipping class, you will need something a bit different:

add_filter( 'woocommerce_package_rates', 'progressive_shipping_cost_based_shipping_class_quantity_steps', 10, 2 );
function progressive_shipping_cost_based_shipping_class_quantity_steps( $rates, $package )
{
    // HERE Bellow your settings
    $shipping_class = "large-shipping"; // The shipping class ID
    $qty_step       = 3; // Items qty threshold for a step
    $item_count     = 0; // Initializing

    // Get the shipping class ID
    $class_id = get_term_by('slug', $shipping_class, 'product_shipping_class' )->term_id;


    // Loop through in cart items to get the Tshirts count
    foreach( $package['contents'] as $cart_item ) {
        if ( $cart_item['data']->get_shipping_class_id() == $class_id ){
            $item_count += $cart_item['quantity']; // Count Tshirts
        }
    }

    // The rate operand increase each {$qty_step} depending on {$item_count}
    $rate_operand = ceil( $item_count / $qty_step );

    foreach ( $rates as $rate_key => $rate ){
        // Targetting "Flat rate"
        if( 'flat_rate' === $rate->method_id ) {
            $has_taxes = false;

            // Set the new cost
            $rates[$rate_key]->cost = $rate->cost * $rate_operand;

            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_key]->taxes as $key => $tax){
                if( $tax > 0 ){
                    // New tax calculated cost
                    $taxes[$key] = $tax * $rate_operand;
                    $has_taxes = true;
                }
            }
            // Set new taxes cost
            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.

Refresh the shipping caches: (required)

  1. This code is already saved on your active theme’s function.php file.
  2. The cart is empty
  3. In a shipping zone settings, disable / save any shipping method, then enable back / save.