WooCommerce PHP Beginner Requires WooCommerce

Add a Handling Fee to Every Order

Add a fixed handling fee, or a percentage of the cart, to every order. Works in the classic and block-based checkout.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 13 lines
add_action( 'woocommerce_cart_calculate_fees', function ( WC_Cart $cart ): void {
	if ( is_admin() && ! wp_doing_ajax() ) {
		return;
	}

	$fixed   = 2.50;  // A fixed amount.
	$percent = 0;     // Or a percentage of the subtotal, e.g. 3 for 3%.

	$fee = $fixed + ( $cart->get_subtotal() * $percent / 100 );
	if ( $fee > 0 ) {
		$cart->add_fee( 'Handling', round( $fee, 2 ), true ); // true = taxable.
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

If packing or processing costs you money on every order, a small handling fee recovers it without raising every product price. This snippet adds a fixed fee, a percentage, or both.

How it works

WooCommerce recalculates fees each time the cart totals change, through woocommerce_cart_calculate_fees. $cart->add_fee() adds a line with a name, an amount and whether it is taxable. The block-based Cart and Checkout use the same calculation, so the fee shows there too.

The first check stops it running in wp-admin outside of cart updates.

Things to watch

  • Show the fee clearly before checkout. Surprise fees are a common reason people abandon carts, and some countries require total prices to be shown up front.
  • Payment-method surcharges are restricted or banned in many places, including the EU and UK. This snippet adds the same fee whatever the payment method.

How to test it

Add an item to the cart. A “Handling” line should appear in the totals, in both the cart and checkout.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only