MyListing Club

Add Surcharge % to All WooCommerce Orders

Building better MyListing websites. One code snippet at a time.

Overview

The Add Surcharge % to All WooCommerce Orders code snippet allows for greater flexibility within a MyListing website.

Out of the box, WooCommerce creates separate endpoints (i.e. pages) for each of these elements, which may not be desirable for website owners.

Implementation

  1. Grab the code snippet.
  2. Follow the provided instructions.
  3. Save your changes.
				
					//// INSTRUCTIONS: 
//// Copy and paste the code snippet below into the child theme's functions.php file.
//// You can edit the functions.php file from your WordPress dashboard (Appearance > Theme Editor) or via FTP.

//// NOTES:
//// The following snippet will add a 5% surcharge to the customer's cart.
//// You can change the 0.05 value to fit your needs.

--------------------------- CODE SNIPPET IS BELOW THIS LINE --------------------------- 

// 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, '' );

}