Your cart is currently empty!
How to Add Dynamic CoD Charges in WooCommerce Based on Cart Value
Why This Is Needed?
If you’re running a WooCommerce store and offer Cash on Delivery (CoD) as a payment method, you’re likely absorbing an extra operational cost — the courier often charges for cash handling. Many sellers either:
- Bear the cost silently, reducing margins, or
- Apply a flat CoD charge regardless of order value — which feels unfair to buyers placing high-value orders.
This is where dynamic CoD fee logic comes in.
Solution: Add Conditional CoD Charges
With this code snippet, you can:
- Charge a certain fixed amount (let’s say ₹49) for orders below ₹2000, Or
- Charge a certain percentage (say 2%) of order total for orders above ₹2000.
Only if the user selects Cash on Delivery as the payment method.
This improves fairness and also recovers part of the CoD cost only from relevant customers.
How to Add This Code to Your WordPress Site
Use the WPCode Plugin
- Install and activate the WPCode plugin.
- Go to
Code Snippets → Add Snippet
. - Choose “Add Your Custom Code (PHP)”.
- Paste the below code and give it a title like “CoD Charges Logic”.
- Set location to “Run everywhere”.
- Save and Activate.
The Code
// Add CoD charges conditionally based on cart value
function add_cod_charges_conditionally() {
if (is_admin() && !defined('DOING_AJAX')) return;
// Only apply if CoD is selected
if (isset(WC()->session) && WC()->session->get('chosen_payment_method') !== 'cod') return;
// Avoid duplicate fees
foreach (WC()->cart->get_fees() as $fee) {
if ($fee->name === 'COD Charges') return;
}
// Define your thresholds
$cart_total = WC()->cart->cart_contents_total;
$cod_charge = ($cart_total < 2000) ? 49 : ($cart_total * 0.0236);
$cod_charge = round($cod_charge, 2);
// Add fee to the cart
WC()->cart->add_fee('COD Charges', $cod_charge, false, 'standard');
}
add_action('woocommerce_cart_calculate_fees', 'add_cod_charges_conditionally');
How to Customize the Code
1. Change the ₹2000 Threshold
$cod_charge = ($cart_total < 2000) ? 49 : ($cart_total * 0.0236);
Change 2000
to any amount like 3000
or 1500
.
2. Change Flat Charge or Percentage
49 // flat fee for lower cart value
0.0236 // 2.36% for higher cart value
Change 49
to another flat fee, or 0.0236
to say 0.015
for 1.5%.
3. Change Fee Label
WC()->cart->add_fee('COD Charges', $cod_charge, false, 'standard');
Change 'COD Charges'
to 'Cash on Delivery Fee'
or 'Cash Handling Charges'
.
Final Thoughts
This tiny customization helps you:
- Recover real-world CoD costs
- Add logic that’s fair and scalable
- Improve buyer trust through transparency
Let me know your thoughts or suggestions.
Leave a Reply