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;
}
Facebook
X
LinkedIn
Email
Print

About the Author

Jason is the founder and owner of Greenville Webworks, a WordPress web design and development company based in Greenville, South Carolina. With over 20 years of experience building websites, he’s worked directly with small and medium-sized businesses across a wide range of industries — from restaurants and nonprofits to e-commerce and professional services — designing modern, mobile-responsive sites built on current web standards and best practices. Jason works hands-on with every client, from initial strategy through launch and ongoing support. Get in touch if you’d like to talk about your next website project.