Introduction
If you’re running an online store with WooCommerce, you may want to customize your shipping options based on country and cart subtotal. For example, you might want to always show shipping options for the US and Canada, but for all other countries, only display shipping if the cart subtotal is $200 or more.
In this guide, we’ll show you exactly how to achieve this using a simple WooCommerce hook.
What You’ll Learn:
-
How to target specific countries for shipping rules.
-
How to set a minimum cart subtotal for showing shipping options.
-
How to add custom PHP code to your WordPress theme.
Step 1: Open Your Theme’s Functions File
To begin, go to your WordPress dashboard:
-
Navigate to Appearance > Theme File Editor.
-
Open the file called
functions.php
(Make sure you are editing a child theme to avoid losing changes after updates).
Step 2: Add This Custom PHP Snippet
Here’s the code you need to paste inside your functions.php
file:
add_filter('woocommerce_package_rates', 'custom_shipping_method_based_on_country_and_subtotal', 10, 2);
function custom_shipping_method_based_on_country_and_subtotal($rates, $package) {
// Get the shipping country
$shipping_country = $package['destination']['country'];
// Get the cart subtotal (excluding taxes and shipping)
$cart_subtotal = WC()->cart->subtotal;
// If country is US or CA, allow shipping methods as normal
if ($shipping_country === 'US' || $shipping_country === 'CA') {
return $rates;
}
// For all other countries, only show shipping methods if subtotal is >= 200
if ($cart_subtotal >= 200) {
return $rates; // Allow shipping methods
} else {
// Hide all shipping methods
return [];
}
}
How the Code Works:
-
It checks the shipping destination country.
-
If the country is US or Canada, it allows all shipping methods as usual.
-
If the country is any other country, it checks if the cart subtotal is at least $200.
-
If not, all shipping options are hidden.
✅ Step 3: Save and Test
-
Save the changes in your
functions.php
file. -
Test your cart:
-
Select the US or Canada – shipping should always appear.
-
Select any other country – shipping should only appear if the cart subtotal is $200 or more.
-
Pro Tip: Make It a Custom Plugin
If you don’t want to lose this code when your theme updates, consider wrapping it into a simple custom plugin. You can create a new .php
file, add plugin headers, and paste the same function inside.
If you need a plugin version, let me know and I can help you build it. 👍