Home / functions.php / Add a % surcharge to your cart / checkout with WooCommerce
  • How to Add a % surcharge to your cart / checkout with WooCommerce

    rename-a-country-in-woocommerce

    If you are a WooCommerce user this great snippet will add a 1% surcharge to your cart on checkout. You can easily change the $percentage variable to adjust the surcharge percent and you are good to go.

    ( click code to copy )

    wordpress snippet : 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, '' );
    }
    

    ( WordPress codex functions, hooks, in this snippet. )

    add_action, is_admin,