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.
Looking for Live Help?
Schedule a live meeting with Drew Angell, PayPal Certified Developer, and get all of your questions or concerns answered.
Featured PayPal Products and Services
-
PayPal Support
$100.00 -
PayPal for WooCommerce
FREE! -
WooCommerce Multiple PayPal Accounts Plugin
FREE! -
PayPal Shipment Tracking for WooCommerce
$49.99 -
Offers for WooCommerce
$59.99 -
WordPress PayPal Invoice Plugin
$20.00 -
PayPal Webhooks for WordPress
$79.99 -
Sale!
PayPal IPN for WordPress
Original price was: $59.99.$49.99Current price is: $49.99.
Hi, thanks for this, just what I need – but when I apply it, the fee is only added to the cart – it doesn’t show up in the checkout. Using latest Woocommerce as of this message. Any thoughts on why this happens? Thanks again
Sorry for the delay getting back to you. Did you figure this out or are you still having issues?
Hello Andrew 😉
I’m trying to implement this little featured but it won’t work on the Twenty Fifteen theme with no plugins but Woocommerce activated. Does it required one of your plugin to function?
Thank you very much!
No, it doesn’t require anything of ours, but the snippet may be out-dated with WooCommerce. It’s been a while since I’ve looked at it. I’ll have to play with it later and see if I have trouble getting it working, and then fix it, of course. Please submit a ticket here to get it into my normal queue of support.
Hello,
How can we make a select with different fees and to apply the selected option?
Thank you!
You can just build that into the snippet we provided in the tutorial. If you’re looking for somebody to build it for you please submit a quote request.
Amazing ! Thanks a lot. I was looking for exactly this thing…you don’t know it, but it saved me four seconds of load time(shitty plugins) 🙂
Ha, happy to help!
Hi,
How to apply this to specific payment gateway?
You would just need to add some basic if/then logic based on the gateway ID, so that the fee is only added accordingly. I don’t have time to provide an example right this moment, but Woo provides Orders and PaymentGateway classes for this.