Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amirulasyraf88/e19da4249effc7786dbae0eaaf456e64 to your computer and use it in GitHub Desktop.
Save amirulasyraf88/e19da4249effc7786dbae0eaaf456e64 to your computer and use it in GitHub Desktop.
Add percentage based fee to WooCommerce cart
<?php
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$subtotal = $woocommerce->cart->subtotal;
$fee_percentage = 0.1; // 10 percent
$fee = number_format( round( $subtotal * $fee_percentage, 2 ), 2 ); // rounded to two decimal places to match currency formatting
$woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment