Skip to content

Instantly share code, notes, and snippets.

@karks88
Last active December 11, 2022 08:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karks88/86ae1d7716ccee4bde96c64196d4d6e9 to your computer and use it in GitHub Desktop.
Save karks88/86ae1d7716ccee4bde96c64196d4d6e9 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: WooCommerce Order Surcharge - Percentage
Plugin URI: https://www.karks.com
Description: Adds a percentage-based surcharge to every order. Based on WooCommerce Snippet: https://docs.woocommerce.com/document/add-a-surcharge-to-cart-and-checkout-uses-fees-api/
Version: 1.0
Author: Eric Karkovack
Author URI: https://www.karks.com
*/
/**
* Add a 3.5% surcharge to your cart / checkout
* change the $percentage to set the surcharge to a value to suit
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.035;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment