304

How to Add a Surcharge Percentage to All WooCommerce Orders

Powered by WPCodeBox

Enhance your WooCommerce store with this code snippet to easily add a surcharge percentage to all orders. This flexible WordPress solution allows you to apply a 5% surcharge to customer carts, with the ability to customize the percentage as needed, offering greater control over your e-commerce pricing.


/**
 * Author: WP Turned UP
 * Author URI: https://wpturnedup.com
 */
 
 // ADD SURCHARGE % TO ALL WOOCOMMERCE ORDERS
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
  global $woocommerce;

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

	$percentage = 0.05;
	$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;	
	$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );

}

Other Snippets

WPCodeBox is a WordPress Code Snippets Manager that allows you to share your WordPress Code Snippets across your sites.