The below code addresses the need to set a maximum shipping cost based on the customer’s shipping country. Specifically, if the customer is in the United States (‘US’), the code dynamically adjusts shipping rates to ensure they do not exceed $10.
/** * Set maximum shipping cost depending on delivery country in WooCommerce */ add_filter( 'woocommerce_package_rates' , 'ts_set_maximum_shipping_cost', 10, 2 ); function ts_set_maximum_shipping_cost( $rates, $package ) { /** * Create an array of countries for a maximum shipping cost * eg array('US','CA'); * for US and Canada. */ $county = array('US'); if ( in_array( WC()->customer->get_shipping_country(), $county ) ) { foreach( $rates as $rate ) { // Change 10 to your maximum shipping cost if( $rate->cost > 10 ) { $rate->cost = 10; } } } return $rates; }
Output
In the below output, the current code sets a maximum of $10 for all shipping rates if the customer is in the United States.
![How to Set Maximum Shipping Cost Based on Delivery Country in WooCommerce? 1 How to Set Maximum Shipping Cost Based on Delivery Country in WooCommerce?](https://media.tychesoftwares.com/wp-content/uploads/maximum-shipping-cost-based-on-delivery-country-1024x752.png)
For other shipping countries, the shipping rates are not modified and shows the regular shipping rates that are set in the backend.
![How to Set Maximum Shipping Cost Based on Delivery Country in WooCommerce? 2 How to Set Maximum Shipping Cost Based on Delivery Country in WooCommerce?](https://media.tychesoftwares.com/wp-content/uploads/shipping-cost-for-other-zones-1024x726.png)
Alternatively, you can also set a minimum order amount based on shipping zone and disabling checkout page if not met helping you in restricting order placement.