While WooCommerce provides built-in coupon management and basic flat-rate shipping rules, advanced e-commerce projects often require dynamic pricing adjustments at checkout. Whether you need to charge a handling fee for small orders, add a payment gateway surcharge, or automatically apply a VIP discount based on the customer’s user role, you need a programmatic approach.
Instead of installing clunky fee plugins that slow down your cart calculations, WooCommerce provides the powerful woocommerce_cart_calculate_fees action hook, allowing you to manipulate cart totals natively.
In this tutorial, we will walk through how to add custom dynamic fees and discounts programmatically.
1. Adding a Conditional Handling Fee Based on Cart Subtotal
To inject a custom fee into the cart and checkout totals, we hook into woocommerce_cart_calculate_fees. This hook fires every time the cart recalculates (e.g., when items are added or shipping changes).
Add the following code to your custom plugin or theme’s functions.php:
if (!defined('ABSPATH')) {
exit;
}
add_action('woocommerce_cart_calculate_fees', 'wg_woo_add_small_order_fee');
function wg_woo_add_small_order_fee($cart) {
// Prevent execution in admin panel unless doing AJAX requests
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$subtotal = $cart->get_subtotal();
// Example: Add a $15.00 handling fee if cart subtotal is under $100.00
if ($subtotal > 0 && $subtotal < 100.00) {
$fee_name = __('Small Order Handling Fee', 'webgarage');
$fee_amount = 15.00;
// Parameters: name, amount, tax_status (boolean), tax_class
$cart->add_fee($fee_name, $fee_amount, true, 'standard');
}
}
2. Applying Dynamic Discounts Based on User Roles
You can also use the same hook to apply negative fees (discounts) dynamically. For instance, if a user belongs to a specific VIP role, you can automatically deduct a percentage from their cart subtotal.
add_action('woocommerce_cart_calculate_fees', 'wg_woo_apply_vip_role_discount', 20, 1);
function wg_woo_apply_vip_role_discount($cart) {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
// Check if user has a custom VIP role
if (in_array('vip_customer', $current_user->roles)) {
$subtotal = $cart->get_subtotal();
// Apply a 10% dynamic discount
$discount_amount = -($subtotal * 0.10);
$cart->add_fee(__('VIP 10% Loyalty Discount', 'webgarage'), $discount_amount, true, '');
}
}
}
3. Persisting Fee Data into Order Meta
When the customer completes checkout and the order is generated, WooCommerce automatically saves any cart fees into the order line items. However, if you need to reference these custom fee amounts programmatically later, they are stored securely within the order fee totals object.
Using the woocommerce_cart_calculate_fees hook allows you to implement complex business logic, handling fees, and automated discounts natively without performance-heavy extensions.
Be sure to check out our previous tutorials on WordPress development:
How to Restrict WordPress REST API via MemberPress
How to Programmatically Create WooCommerce Orders
How to Programmatically Create MemberPress Subscriptions
Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.