WooCommerce’s default shipping rules are quite straightforward, which is great for most small stores. However, as your business grows, you may encounter complex shipping requirements that the default settings cannot handle. While there are excellent plugins like WooCommerce Weight Based Shipping for weight-dependent costs, you can also leverage WooCommerce’s built-in hooks to implement custom shipping logic tailored to your specific needs.
For example, let’s say you have the following shipping requirement: shipping is $12 for up to 5 items. For every additional 5 items (or portion thereof), an extra $4 shipping fee is added. If a customer buys 6-10 items, the shipping becomes $16; for 11-15 items, it’s $20, and so on.
Analyzing this logic, if we let X be the number of items in the cart, the formula for the final shipping cost is: 12 + ceil((X - 5) / 5) * 4. Since the formula is relatively simple, we can modify the standard “Flat Rate” shipping method in WooCommerce to incorporate this calculation.
1. Adding Custom Settings to the Flat Rate Shipping Method
First, we’ll add a new field to the Flat Rate shipping settings area. This allows store managers to easily adjust the additional surcharge amount directly from the WooCommerce dashboard.
/**
* Add a custom 'Cost Addon' field to Flat Rate shipping settings.
*/
add_filter('woocommerce_shipping_instance_form_fields_flat_rate', function ($fields)
{
$fields['cost_addon'] = [
'title' => __('Additional Shipping per 5 Items', 'woocommerce'),
'type' => 'text',
'placeholder' => '',
'default' => '',
'desc_tip' => true,
];
return $fields;
});
2. Calculating the Final Shipping Cost
Now that we have the setting, we can use the woocommerce_flat_rate_shipping_add_rate hook to apply our custom formula. This code calculates the total shipping fee based on the quantities in the cart and the values defined in the settings.
/**
* Calculate and apply the shipping rate based on item quantity.
*/
add_action('woocommerce_flat_rate_shipping_add_rate', function ($flat_rate_class, $rate)
{
global $woocommerce;
$cart = $woocommerce->cart;
$package = $cart->get_shipping_packages();
$quantity = $cart->cart_contents_count;
$basic_cost = $flat_rate_class->get_option('cost');
$addon_cost = $flat_rate_class->get_option('cost_addon');
$addon_qty = $quantity - 5;
if ($addon_qty > 0 && ! empty($addon_cost)) {
// Calculate how many 5-item blocks are beyond the initial 5
$addon_charge_qty = ceil($addon_qty / 5);
$total_addon = $addon_cost * $addon_charge_qty;
$custom_rate = [
'id' => $flat_rate_class->get_rate_id(),
'label' => __('Flat Rate (Calculated)', 'woocommerce'),
'cost' => $basic_cost + $total_addon,
'taxes' => false,
'calc_tax' => 'per_order',
'meta_data' => [],
'package' => $package,
'price_decimals' => wc_get_price_decimals(),
];
// Replace the default rate with our custom calculated rate
$flat_rate_class->add_rate($custom_rate);
}
}, 10, 2);
By using these two hooks, we’ve successfully added a dynamic pricing layer to WooCommerce’s default shipping. While using a dedicated plugin is often the most convenient way to add functionality, knowing how to use hooks gives you the power to implement highly specific business logic when off-the-shelf solutions don’t quite fit. If you find yourself needing more than the basics, it’s always worth checking for available hooks before diving into a full custom implementation.
