Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

woocommerce_cart_calculate_fees

Fires while cart totals are worked out, so you can add fees or surcharges to the cart with $cart->add_fee().

  • Type Action
  • From WooCommerce
  • Since 2.0.14
  • Fired in includes/class-wc-cart.php

When it fires

WooCommerce fires this action every time it recalculates the cart totals, after item and shipping totals and before the grand total. Fees are cleared at the start of each calculation, so add yours every time the hook runs.

It runs for the classic cart and checkout and for the Cart and Checkout blocks, and it can run several times in one request.

Parameters

Parameters
$cartWC_Cart The cart object.

Example: Add a small order fee

PHP
add_action( 'woocommerce_cart_calculate_fees', function ( $cart ) {
	if ( is_admin() && ! wp_doing_ajax() ) {
		return;
	}
	if ( $cart->get_subtotal() >= 20 ) {
		return;
	}
	$cart->add_fee( __( 'Small order fee', 'my-snippets' ), 2.5, true );
} );

Good to know

  • The third argument of add_fee() makes the fee taxable. Each fee needs a unique name; adding the same name twice is refused.
  • For discounts, coupons are usually a better fit than negative fees.
  • Thinking of a surcharge for paying by card? Check the rules first: they're banned for most consumer payments in the UK and the EU.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.