Add a fixed handling fee, or a percentage of the cart, to every order. Works in the classic and block-based checkout.
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.
}
} );
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.
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.
Add an item to the cart. A “Handling” line should appear in the totals, in both the cart and checkout.