Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
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.

/**
 * 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
0
Would love your thoughts, please comment.x
()
x