woocommerce custom surcharge all transactions

Instructions

Snippet Type: PHP

Copy the snippet.
Add the snippet using WPBox.
Follow any customization notes provided.
Save the snippet.
Enable the snippet.
Test.

Snippet


This snippet will add a 2% surcharge to the customer’s cart, but you can change the 0.02 value as desired.


add_action( 'woocommerce_cart_calculate_fees', 'woocommerce_custom_surcharge' );

function woocommerce_custom_surcharge() {

    global $woocommerce;

 if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;

 $percentage = 0.02;

    $surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;

    $woocommerce->cart->add_fee( 'Credit Card Security And Compliance Fee', $surcharge, true, '' );

}