Recently I was contacted by a user of our PayPal for WooCommerce extension to inform me of a bug in the plugin. Custom fees were not getting included in the cart totals. As such, PayPal would return an invalid cart totals error upon checkout. Doh!

I needed a quick way to easily add custom fees to the shopping cart so that I could test this issue and get it resolved, which has been done as of our 1.1.3 version update.

I was provided with the following snippet to do just that. Simply placing this in my child theme’s functions.php file causes a $2.00 fee to be applied to all orders.

// Add Fee to all products
function woo_add_cart_fee()
	global $woocommerce;
	$additional_fee_name = "Service Fee";
	$extra_fee = 2;
	$additional_fee_taxable = true;

	$addedFee = false;
	// first check to make sure it isn’t already there
	foreach ( $woocommerce->cart->get_fees() as $_fee )
		if ($_fee->id == sanitize_title($additional_fee_name) )
		$_fee->amount = (float) esc_attr( $extra_fee );
		$_fee->taxable = $additional_fee_taxable;
		$addedFee = true;
	if (!$addedFee)
		$woocommerce->cart->add_fee( __($additional_fee_name, "woocommerce"),
		$extra_fee, $additional_fee_taxable );
add_action( "woocommerce_before_calculate_totals", "woo_add_cart_fee" );

Of course, this can be manipulated to handle any sort of custom fee you might need to add to your orders, and I found it very useful for testing and troubleshooting the fees.

Need Additional Help?

Schedule a live meeting with Drew Angell, PayPal Certified Developer, and get all of your questions or concerns answered.

AngellEYE PayPal Newsletter Signup

Subscribe To Our Newsletter

Join our list for the latest promotions, news, and updates from AngellEYE and PayPal!

You have Successfully Subscribed!

Pin It on Pinterest