Set Maximum Shipping Charge for Woocommerce

This is a simple way to set a maximum shipping cost for your woocommerce orders to prevent Shipping rates from becoming too high and scaring away customers. You can set the domestic and international shipping rate maximums separately.

If you have several shipping classes that are added together, if a customer purchases items from multiple shipping classes, the shipping may go higher than you want, so this is a good way to put a hard cap on how much is charged for shipping.

Copy and paste the code below into your theme’s functions.php file. Make sure you aren’t using any other “woocommerce_package_rate” filters, if so, they may interfere.

add_filter('woocommerce_package_rates', 'set_max_shipping_rate', 10, 3);
function set_max_shipping_rate( $available_shipping_methods, $package ){
    $origin_country = 'US'; 
    $max_amount_domestic = 12.99; // Set max shipping cost for domestic shipments.
    $max_amount_international = 12.99; // Set max shipping cost for International shipments.

    $max_amount = ($package['destination']['country'] == $origin_country) ? $max_amount_domestic : $max_amount_international;

    $item_count = 0;
    foreach ($package['contents'] as $key => $item) {
        $item_count += $item['quantity'];
    }

    foreach ($available_shipping_methods as $methord_name => $methord) {
        if( $max_amount < $available_shipping_methods[$methord_name]->cost ){
            $available_shipping_methods[$methord_name]->cost = $max_amount;
        }
    }
    return $available_shipping_methods;
}