Add Fee and update total in WooCommerce

This PHP code enables you to calculate a percentage of the total cart value including shipping, taxes and other fees and update the total.

In this example, we add 2.5% to the gross total.

Copy & paste the PHP code to the end of your child themes functions.php file or code snippets plugin.

add_action('woocommerce_cart_calculate_fees', 'add_processing_fee_to_cart');
function add_processing_fee_to_cart() {
    global $woocommerce;

    // Get the cart total excluding taxes and other fees
    $cart_total = WC()->cart->get_cart_contents_total();

    // Add taxes
    $cart_total += WC()->cart->get_taxes_total();

    // Calculate the processing fee (2.5% of the cart total)
    $processing_fee = $cart_total * 0.025;

    // Add the processing fee to the cart
    WC()->cart->add_fee(__('Processing Fee', 'text-domain'), $processing_fee, true, '');

Code Modification

Swap out the 0.025 in the code to match your own percentage.

The code can be modified and extended to do anything you need.

Need More Coding Help?

Book a Consultation