Skip to content

Instantly share code, notes, and snippets.

@jorpdesigns
Last active July 15, 2021 14:32
Show Gist options
  • Save jorpdesigns/ba35de2aafebf15edc067207844f0c04 to your computer and use it in GitHub Desktop.
Save jorpdesigns/ba35de2aafebf15edc067207844f0c04 to your computer and use it in GitHub Desktop.
Snippet to add fee to WooCommerce cart
<?php
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ADD FLAT RATE FEE
$surcharge = 5; // Replace 5 with your fee
// ADD FEE AS PERCENTAGE OF CART TOTAL
$percentage = 0.15; // Replace 0.15 with your percentage
$surcharge = ceil( $cart->cart_contents_total * $percentage );
$cart->add_fee( 'Fee Label', $surcharge, true, '' );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment