-
-
Save ChromeOrange/5770314 to your computer and use it in GitHub Desktop.
| /** | |
| * 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, '' ); | |
| } |
It does not update the order total.
Fixed that
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 ?
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.
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
Having same PayPal issue as madamelolo... Any ideas?
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' );
}Hi, thanks for this snippet.
Is it possible to add the fee based on the chosen payment method?
Corrected to remove 'standard' from add_fee based on woocommerce/woocommerce#6808
Where do i post this code ?
How would I make the fee/surcharge populate based on a minimum cart total amount...say $500...?
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.
Were you able to get the value to post to the total?