Free Delivery Fee Logic
March 24, 2025 · AI Conversations
—
### ✅ Updated `apply_custom_delivery_fee` function:
add_action('woocommerce_cart_calculate_fees', 'apply_custom_delivery_fee');
function apply_custom_delivery_fee($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
$fees = [
'TINAHELY' => 5, 'ARKLOW' => 18, 'ANNACURRA' => 8, 'AUGHRIM' => 12,
'BALLINGLEN' => 6, 'BALLYCONNELL' => 12, 'CARNEW' => 10,
'COOLKENNO' => 10, 'HACKETSTOWN' => 10, 'GOREY' => 18,
'KILTEGAN' => 15, 'KNOCKANANNA' => 10, 'RATHDRUM' => 18,
'SHILLELAGH' => 7, 'TULLOW' => 18
];
$selected = WC()->session->get('custom_delivery_area');
// ✅ Check if cart total is above 55
if ($cart->subtotal_ex_tax >= 55) {
return; // Don't apply delivery fee
}
if ($selected && isset($fees[$selected])) {
$cart->add_fee('Delivery to ' . $selected, $fees[$selected]);
}
}
—
### 👀 Why use `$cart->subtotal_ex_tax`?
– It **excludes taxes and shipping fees**, checking **only the actual product subtotal**.
– If you'd rather check **including discounts**, use `$cart->get_subtotal()` instead.
– If you want to check the **final total after discounts**, use `$cart->get_total('edit')`.
Let me know if you’d like to apply the free delivery **after discount** instead — I can tweak that logic for you.
—
### ✅ Updated `apply_custom_delivery_fee` function:
add_action('woocommerce_cart_calculate_fees', 'apply_custom_delivery_fee');
function apply_custom_delivery_fee($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
$fees = [
'TINAHELY' => 5, 'ARKLOW' => 18, 'ANNACURRA' => 8, 'AUGHRIM' => 12,
'BALLINGLEN' => 6, 'BALLYCONNELL' => 12, 'CARNEW' => 10,
'COOLKENNO' => 10, 'HACKETSTOWN' => 10, 'GOREY' => 18,
'KILTEGAN' => 15, 'KNOCKANANNA' => 10, 'RATHDRUM' => 18,
'SHILLELAGH' => 7, 'TULLOW' => 18
];
$selected = WC()->session->get('custom_delivery_area');
if ($selected && isset($fees[$selected])) {
if ($cart->subtotal_ex_tax >= 55) {
$cart->add_fee('Free Delivery to ' . $selected, 0);
} else {
$cart->add_fee('Delivery to ' . $selected, $fees[$selected]);
}
}
}
—
### 💡 Optional: Style the free delivery fee on the checkout page
You can also use custom CSS or filters if you want to highlight free delivery in green or bold — just let me know if you'd like that too.
Let me know if you want this logic to be **based on order total including tax or discount** instead of `subtotal_ex_tax`.