To set up custom shipping fees in WooCommerce based on the billing country, you can use a combination of WooCommerce’s built-in functionality and custom code. Here’s a step-by-step guide to help you achieve this:
Step 1: Create Shipping Zones
In WooCommerce, you can define shipping zones to group countries or regions with similar shipping rules. Go to WooCommerce > Settings > Shipping > Shipping Zones. Create a new shipping zone and add the countries you want to customize the shipping fees for.
Step 2: Set Up Shipping Methods
Within each shipping zone, you can set up multiple shipping methods. For this example, let’s assume you want to set up a flat rate shipping fee per country. Click on the “+ Add Shipping Method” button and choose “Flat Rate” from the dropdown.
Step 3: Configure Flat Rate Shipping Method
In the configuration options for the Flat Rate shipping method, you’ll find a field labeled “Method Title.” Here, you can enter a descriptive name for the shipping method, such as “Custom Shipping Fee by Country.”
Step 4: Configure Shipping Costs
In the Flat Rate shipping method configuration, you’ll also find a field labeled “Cost.” You can enter the default shipping fee for all countries within the shipping zone.
Step 5: Add Custom Code
Custom Shipping Rates by billing country Programmatically
To override the default shipping fee based on the billing country, you’ll need to add some custom code to your theme’s functions.php file or use a custom plugin like Code Snippets.
Here’s an example of how you can modify the shipping fee based on the billing country:
function custom_shipping_fee_by_billing_country($rates, $package) {
// Get the customer's billing country
$billing_country = WC()->customer->get_billing_country();
// Modify shipping fee based on the billing country
switch ($billing_country) {
case 'US':
// Modify the shipping cost for the United States
$rates['flat_rate']->cost = 10; // Change the cost to your desired amount
break;
case 'CA':
// Modify the shipping cost for Canada
$rates['flat_rate']->cost = 15; // Change the cost to your desired amount
break;
default:
$rates['flat_rate']->cost = 0; // Default shipping fee if no specific country match found
break;
}
return $rates;
}
add_filter('woocommerce_package_rates', 'custom_shipping_fee_by_billing_country', 10, 2);
In the example above, the custom_shipping_fee_by_billing_country function checks the customer’s billing country and modifies the shipping fee accordingly. Customize the code snippet according to your needs. In the switch statement, add cases for each country you want to set custom shipping fees for, and set the appropriate shipping cost for each case.
Remember to replace the country codes (e.g., ‘US’, ‘CA’) and the shipping costs with your specific values.
That’s it! With these steps, you can set up custom shipping fees in WooCommerce based on the billing country.