WooCommerce Custom Flat Rate Shipping Based on Product Quantity and Free Shipping
When you want more control over shipping rates in WooCommerce, the default options often fall short. In this guide, we’ll walk you through a custom solution that automatically increases the shipping cost based on the number of products in the cart and hides flat rate shipping when free shipping is available.
This solution is perfect if you:
-
Want to charge extra shipping fees per product.
-
Want to hide flat rate shipping when customers qualify for free shipping.
Let’s dive in! 🚀
🎯 What We’re Building
-
If the cart has only one product, apply the default flat rate.
-
If the cart has more than one product, add $3 per additional product (excluding the first).
-
If free shipping is available, hide flat rate shipping entirely.
✅ Full WooCommerce Shipping Customization Code
Add the following code to your theme’s functions.php
file or a custom plugin:
add_filter('woocommerce_package_rates', 'custom_flat_rate_shipping_with_extra_per_product', 10, 2);
function custom_flat_rate_shipping_with_extra_per_product($rates, $package) {
// Get total quantity of products in the cart
$cart_item_count = WC()->cart->get_cart_contents_count();
// Flag to check if free shipping is available
$free_shipping_available = false;
// Check if free shipping is present in the rates
foreach ($rates as $rate) {
if (strpos($rate->method_id, 'free_shipping') !== false) {
$free_shipping_available = true;
break;
}
}
foreach ($rates as $rate_key => $rate) {
// Check if this is the flat rate shipping method
if (strpos($rate->method_id, 'flat_rate') !== false) {
// If free shipping is available, remove flat rate option
if ($free_shipping_available) {
unset($rates[$rate_key]);
continue; // Skip further processing
}
// If more than one product, add extra cost
if ($cart_item_count > 1) {
// Add $3 for each additional product (excluding the first)
$additional_cost = ($cart_item_count - 1) * 3;
// Update the shipping cost
$rates[$rate_key]->cost += $additional_cost;
// Optional: Update taxes if necessary
if (!empty($rates[$rate_key]->taxes)) {
foreach ($rates[$rate_key]->taxes as $tax_id => $tax_amount) {
$additional_tax = ($additional_cost) * ($tax_amount / $rate->cost);
$rates[$rate_key]->taxes[$tax_id] += $additional_tax;
}
}
}
}
}
return $rates;
}
🔧 Explanation:
-
Flat Rate Detection: The code checks if the shipping method is a flat rate.
-
Free Shipping Check: If free shipping is available, the flat rate option is automatically hidden.
-
Dynamic Pricing: Adds $3 for each additional product in the cart beyond the first.
🚀 Why This is Useful:
-
Keeps your shipping pricing flexible and competitive.
-
Encourages larger orders by offering clear shipping cost logic.
-
Improves user experience by automatically hiding unnecessary options.
📚 Final Thoughts:
WooCommerce is powerful, but with a bit of custom code, you can take your store to the next level. This shipping customization is an excellent example of how to fine-tune the shopping experience to match your store’s policies and pricing structure.
If you want more WooCommerce customizations like this, keep following Codrammer.com for detailed guides, expert tips, and practical solutions.