Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active July 3, 2017 01:18
Show Gist options
  • Select an option

  • Save ChromeOrange/5770314 to your computer and use it in GitHub Desktop.

Select an option

Save ChromeOrange/5770314 to your computer and use it in GitHub Desktop.
Add a surcharge to the cart and checkout
/**
* Add a 1% surcharge to your cart / checkout
* change the $percentage to set the surcharge to a value to suit
* Uses the WooCommerce fees API
*
* Add to theme functions.php
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
@dademaru
Copy link

Hi, thanks for this snippet.
Is it possible to add the fee based on the chosen payment method?

@ChromeOrange
Copy link
Author

Corrected to remove 'standard' from add_fee based on woocommerce/woocommerce#6808

@maximizepromotions
Copy link

Where do i post this code ?

@seanannnigans
Copy link

How would I make the fee/surcharge populate based on a minimum cart total amount...say $500...?

@jumpstart17
Copy link

Was anyone able to find an answer to not have the surcharge be added to one product at all? I want it added to all products exept one of them. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment