Skip to content

Instantly share code, notes, and snippets.

@ChromeOrange
Last active July 3, 2017 01:18
Show Gist options
  • Select an option

  • Save ChromeOrange/5770314 to your computer and use it in GitHub Desktop.

Select an option

Save ChromeOrange/5770314 to your computer and use it in GitHub Desktop.
Add a surcharge to the cart and checkout
/**
* Add a 1% surcharge to your cart / checkout
* change the $percentage to set the surcharge to a value to suit
* Uses the WooCommerce fees API
*
* Add to theme functions.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, '' );
}
@ChromeOrange
Copy link
Author

Fixed that

@madamelolo
Copy link

I am having an issue with a version of this code based on % of shipping. I get an error message when going to Paypal , when I add this to my theme functions.php file.
/**

  • Add a 20% surcharge to shipping only-- fuel surcharge
    */
    add_action( 'woocommerce_cart_calculate_fees','solstiss_custom_surcharge' );
    function themename_custom_surcharge() {
    global $woocommerce;

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

    $percentage = 0.20;
    $surcharge = ( $woocommerce->cart->shipping_total ) * $percentage;
    $woocommerce->cart->add_fee( __( 'Fuel surcharge', 'themename' ), $surcharge, true, 'standard' );

}
Any idea what is wrong with it ?

@thisisbbc
Copy link

Great code snippet!
Works like a charm. However, I'm trying to modify it slightly so that the surcharge is only applied to a product category, and it doesn't seem like an easy task... Any help would be gladly appreciated.
The expected result is to add a 5% fee on all products in a specific category. The current logic adds a fee to the cart total, whereas it should calculate the fee for each item.

@kensuarez
Copy link

on this part:
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;

is there a way to specify "cart_contents_total" to a certain item/category only? like example "product_contents_total('Hotels')", something like that

thanks in advance

@mandiees
Copy link

Having same PayPal issue as madamelolo... Any ideas?

@mandiees
Copy link

Hi Everyone,

The reason this is happening is due to a rounding error - PayPal can only accept 2 decimal places for the surcharge.

Below is the code that worked for me:

/**
 * Add a 1% surcharge to your cart / checkout
 * change the $percentage to set the surcharge to a value to suit
 * Uses the WooCommerce fees API
 *
 * Add to theme functions.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;    
    $surcharge = round( $surcharge, 2 );

    $woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, 'standard' );

}

@dademaru
Copy link

Hi, thanks for this snippet.
Is it possible to add the fee based on the chosen payment method?

@ChromeOrange
Copy link
Author

Corrected to remove 'standard' from add_fee based on woocommerce/woocommerce#6808

@maximizepromotions
Copy link

Where do i post this code ?

@seanannnigans
Copy link

How would I make the fee/surcharge populate based on a minimum cart total amount...say $500...?

@jumpstart17
Copy link

Was anyone able to find an answer to not have the surcharge be added to one product at all? I want it added to all products exept one of them. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment