skip to Main Content

How to Add Custom Fees to Your WooCommerce Checkout


Table of Contents

  1. Overview
  2. Understanding the Code
  3. Adding a Custom Fixed Fee
  4. Adding Tax Class
  5. Adding a Custom Percentage Fee
  6. Customizing the Fee Logic
  7. Adding a Fee Based on User Role
  8. Adding a Fee for Specific Products
  9. Offering a Discount
  10. Adding a Fee for Specific Shipping Methods
  11. Seasonal or Promotional Fees
  12. Fee Based on Product Category
  13. Fee Based on Total Quantity of Products
  14. Fee Based on Product Attribute
  15. Fee Based on Custom Field
  16. Fee Based on Total Number of Products
  17. Testing Your Custom Fees
  18. Conclusion
  19. FAQ

Overview

Adding custom fees to your WooCommerce checkout can help you meet various business needs. For example, you might want to implement shipping surcharges, handling fees, or promotional charges. In this guide, we will walk through how to implement custom fees programmatically. We will cover options for both fixed and percentage-based fees. Let’s get started!


Understanding the Code

WooCommerce provides the woocommerce_cart_calculate_fees action hook. This hook allows you to modify the cart’s total by adding custom fees. We will use this hook to implement our custom fee functionality.


Adding a Custom Fixed Fee

Here’s a simple example of how to add a fixed custom fee of $20 to the cart:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_add_custom_fee');
function codebykishor_add_custom_fee() {
    $custom_fee = 20; // Set the desired fee amount
    WC()->cart->add_fee('Custom Fee', $custom_fee, true, 'standard');
}

Adding Tax Class

If you want the custom fee to be taxable, specify the tax class. In the example above, we used ‘standard’. You can create custom tax classes in WooCommerce settings if needed.


Adding a Custom Percentage Fee

If you prefer to charge a fee as a percentage of the cart total, modify the function like this:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_add_custom_percentage_fee');
function codebykishor_add_custom_percentage_fee() {
    $percentage_fee = 10; // 10% fee
    $cart_total = WC()->cart->subtotal; // Get the cart subtotal
    $custom_fee = ($percentage_fee / 100) * $cart_total; // Calculate the fee as a percentage
 
    // Add the fee
    WC()->cart->add_fee('Percentage Fee (10%)', $custom_fee, true, 'standard');
}

Customizing the Fee Logic

You can also add conditions to apply fees based on various criteria. For example, you might want to apply a fee only if the cart total exceeds a certain amount:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_add_custom_fee_with_condition');
function codebykishor_add_custom_fee_with_condition() {
    $threshold = 100; // Set the threshold amount
    if (WC()->cart->subtotal > $threshold) {
        $custom_fee = 20; // Fee amount
        WC()->cart->add_fee('Custom Fee', $custom_fee, true, 'standard');
    }
}


Adding a Fee Based on User Role

You might want to charge different fees based on the user’s role. For instance, you could add a fee for guest users while offering free shipping to logged-in customers.

add_action('woocommerce_cart_calculate_fees', 'codebykishor_role_based_fee');
function codebykishor_role_based_fee() {
    if (!is_user_logged_in()) {
        $custom_fee = 10; // $10 fee for guests
        WC()->cart->add_fee('Guest Fee', $custom_fee, true, 'standard');
    }
}


Adding a Fee for Specific Products

You might want to add a custom fee when certain products are in the cart. For instance, if a user buys a specific product, you can charge an additional handling fee.

add_action('woocommerce_cart_calculate_fees', 'codebykishor_product_based_fee');
function codebykishor_product_based_fee() {
    $fee = 0; // Initialize fee variable
    $target_product_id = 123; // Replace with your product ID

    foreach (WC()->cart->get_cart() as $cart_item) {
        if ($cart_item['product_id'] === $target_product_id) {
            $fee = 5; // $5 handling fee for this product
            break; // Exit loop once the product is found
        }
    }

    if ($fee > 0) {
        WC()->cart->add_fee('Handling Fee', $fee, true, 'standard');
    }
}

Offering a Discount

You can also use this hook to apply discounts under certain conditions. For instance, if the cart total exceeds a certain amount, you can offer a discount.

add_action('woocommerce_cart_calculate_fees', 'codebykishor_cart_discount');
function codebykishor_cart_discount() {
    $threshold = 100; // Set the threshold amount
    $discount = 10; // Discount amount

    if (WC()->cart->subtotal > $threshold) {
        WC()->cart->add_fee('Discount', -$discount, true, 'standard');
    }
}

Adding a Fee for Specific Shipping Methods

You might want to add a fee depending on the selected shipping method. For example, if a customer chooses express shipping, you can apply an additional fee.

add_action('woocommerce_cart_calculate_fees', 'codebykishor_shipping_method_fee');
function codebykishor_shipping_method_fee() {
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    
    if (!empty($chosen_methods) && $chosen_methods[0] === 'flat_rate:1') { // Replace with your shipping method ID
        $custom_fee = 15; // $15 fee for express shipping
        WC()->cart->add_fee('Express Shipping Fee', $custom_fee, true, 'standard');
    }
}


Seasonal or Promotional Fees

You can apply custom fees based on seasonal promotions or specific campaigns. For instance, during a holiday season, you could add a special handling fee.

add_action('woocommerce_cart_calculate_fees', 'codebykishor_seasonal_fee');
function codebykishor_seasonal_fee() {
    $current_month = date('n'); // Get current month number

    if ($current_month === 12) { // If it's December
        $custom_fee = 20; // $20 holiday fee
        WC()->cart->add_fee('Holiday Fee', $custom_fee, true, 'standard');
    }
}

Fee Based on Product Category

This applies a fee if any product in the cart is from a specific category (e.g., category slug: custom-category):

add_action('woocommerce_cart_calculate_fees', 'codebykishor_category_based_fee');
function codebykishor_category_based_fee($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $target_category = 'custom-category'; // Replace with your category slug
    $fee = 0;

    foreach ($cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];
        if (has_term($target_category, 'product_cat', $product_id)) {
            $fee = 5; // Apply $5 fee if category is found
            break;
        }
    }

    if ($fee > 0) {
        $cart->add_fee(__('Handling Fee', 'woocommerce'), $fee, true);
    }
}


Fee Based on Total Quantity of Products

This version charges based on the number of products in the cart. For example, $2 per item:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_quantity_based_fee');
function codebykishor_quantity_based_fee($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $total_quantity = $cart->get_cart_contents_count(); // Total quantity in cart
    $fee_per_item = 2; // $2 per item
    $total_fee = $total_quantity * $fee_per_item;

    if ($total_quantity > 0) {
        $cart->add_fee(__('Item Handling Fee', 'woocommerce'), $total_fee, true);
    }
}


Fee Based on Product Attribute (e.g., Size = Large)

Let’s say you use a product attribute like Size (pa_size) and want to charge a fee only for “Large” size products:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_size_based_fee');
function codebykishor_size_based_fee($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $fee_per_large_item = 3; // $3 fee per "Large" size product
    $total_fee = 0;

    foreach ($cart->get_cart() as $cart_item) {
        $product = $cart_item['data'];
        $size = $product->get_attribute('pa_size'); // Replace 'pa_size' with your actual attribute slug

        if (strtolower($size) === 'large') {
            $total_fee += $fee_per_large_item * $cart_item['quantity'];
        }
    }

    if ($total_fee > 0) {
        $cart->add_fee(__('Size Handling Fee', 'woocommerce'), $total_fee, true);
    }
}



Fee Based on Custom Field (ACF, Meta Field, etc.)

If you’re using a custom field (e.g., via ACF plugin), and you store size info in a meta field like _product_size, use this:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_custom_size_fee');
function codebykishor_custom_size_fee($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $fee_per_large_item = 3; // $3 fee per "Large" size item
    $total_fee = 0;

    foreach ($cart->get_cart() as $cart_item) {
        $product_id = $cart_item['product_id'];
        $size = get_post_meta($product_id, '_product_size', true); // Replace with your custom field key

        if (strtolower($size) === 'large') {
            $total_fee += $fee_per_large_item * $cart_item['quantity'];
        }
    }

    if ($total_fee > 0) {
        $cart->add_fee(__('Custom Size Fee', 'woocommerce'), $total_fee, true);
    }
}

Fee Based on Total Number of Products (Any Product)

Example: Charge $2 per item in the cart, regardless of product type:

add_action('woocommerce_cart_calculate_fees', 'codebykishor_fee_per_product_quantity');
function codebykishor_fee_per_product_quantity($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    $fee_per_item = 2; // $2 per product
    $total_quantity = $cart->get_cart_contents_count(); // Total number of items
    $total_fee = $fee_per_item * $total_quantity;

    if ($total_quantity > 0) {
        $cart->add_fee(__('Per Product Fee', 'woocommerce'), $total_fee, true);
    }
}


Testing Your Custom Fees

After adding your custom fee code to your theme’s functions.php file or a custom plugin:

  1. Go to your WooCommerce store.
  2. Add products to your cart.
  3. Proceed to the checkout page and ensure your custom fee appears as expected.

Conclusion

Adding custom fees to your WooCommerce checkout can enhance your store’s functionality and provide a better shopping experience for your customers. Whether you choose a fixed fee or a percentage-based fee, the flexibility provided by WooCommerce allows you to tailor your checkout process to your needs.

If you have any doubts or something isn’t working as expected, please contact us or leave a comment below.


FAQ

What is the woocommerce_cart_calculate_fees hook?

It allows you to add or modify fees during WooCommerce checkout.

Can I add multiple custom fees?

Yes, you can add multiple fees in one function or separate functions.

How do I conditionally add fees based on user roles?

Use is_user_logged_in() to check roles and apply fees accordingly.

Can I apply fees for specific products?

Yes, loop through cart items and check for specific product IDs.

Are custom fees displayed in the order summary?

Yes, custom fees will be displayed in the order summary during checkout, and they will be included in the total amount.


I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

This Post Has 0 Comments

Leave a Reply

Back To Top