Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
00 Days
00 Hours
00 Minutes
00 Seconds

How to Set Maximum Shipping Cost Based on Delivery Country in WooCommerce?

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* 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;
}
/** * 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; }
/**
 * 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?

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?

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.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of


0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
reCaptcha Error: grecaptcha is not defined
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.