Skip to content

Instantly share code, notes, and snippets.

@corsonr
Last active April 9, 2022 15:38
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save corsonr/6182904 to your computer and use it in GitHub Desktop.
Save corsonr/6182904 to your computer and use it in GitHub Desktop.
WooCommerce - Add custom fee to cart
<?php
/**
* WooCommerce Extra Feature
* --------------------------
*
* Add custom fee to cart automatically
*
*/
function woo_add_cart_fee() {
global $woocommerce;
$woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
@rsharrer
Copy link

I am using something similar, but I only want it to apply if the order total is under $50.00, but I am not sure what I am doing wrong

/* add custom price for small order fee */
add_action( 'woocommerce_before_calculate_totals', 'cp_check_total' );

function cp_check_total( $subtotal ) {
global $woocommerce;

$subtotal = $woocommerce->cart->get_cart_subtotal();

if($subtotal < 50.00) {
$excost = 4.95;
$woocommerce->cart->add_fee( 'Small Order Fee', $excost, $taxable = false,'');

}

}

it seems to work the same as your it is like it is not accepting the if statement, any help is greatly appricated.

@terrytsang
Copy link

@salvatorifabio77
Copy link

I would like to apply this function to the plugin product gift wrap so as to obtain an additional cost for product free from VAT

@chingychun
Copy link

sorry for being a complelte noob, but where do I add this code to?

@muzzika88
Copy link

@chungychun: add it to your functions.php for wordpress

Can I change the fee into a dropdown?

@nmedia82
Copy link

Hi,

following plugin is to add options fee into cart total against each total.
http://www.najeebmedia.com/n-media-woocommerce-personalized-checkout-options-manager/

@thecodepoetry
Copy link

The hook si now woocommerce_cart_calculate_fees
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

I wrote some examples here
http://www.thecodepoetry.com/update-woocommerce-cart-price-with-code/

@ronlum
Copy link

ronlum commented Nov 18, 2014

@thecodepoetry: Thanks for the update.

@corsonr: Thank you for this simple, yet effective code snippet. You are a beautiful man.

Tip: For anyone wanting a dynamic custom fee that is based on the subtotal, you can do that too! Just assign a variable like so:

$subt = $woocommerce->cart->subtotal;

Then you can modify that variable as you like. And just replace the '5' with your variable ($subt) like so:

$woocommerce->cart->add_fee( __('Difference', 'woocommerce'), $subt);

@shizzel
Copy link

shizzel commented Dec 12, 2014

hi corsonr, i need to be able to choose which products have the extra fee on, is that possible with the pro version of the plugin re;

“Cart Items” – you can set cart item range for the fee

?

@stimart
Copy link

stimart commented Jan 18, 2015

@greguly
Copy link

greguly commented Oct 3, 2015

The above link is for members only, not nice.

@greguly
Copy link

greguly commented Oct 3, 2015

New version for WC 2.4

<?php
/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Add custom fee to cart automatically
 *
 */
function woo_add_cart_fee() {

  WC()->cart->add_fee( __('Custom', 'woocommerce'), 5 );

}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

@manishjindal2020
Copy link

this code working correctly with woocommece, but does not work with WooSubscriptions please solve problem

@Maggieylau
Copy link

I'm using the Woocommerce Extra Fee Option plugin and I was wondering if there's a way to make the extra fee update to new subtotal after a coupon is entered in. For example, if the product is $100 and the extra fee is 5% then it would tack on an extra $5 but if I enter in a couple for $50 off then the fee should update to be $2.5. Is this possible?

@spiderflystudios
Copy link

My question is related to this...as in it is about fees... You know how you can manually add fees to an order when you manually create an order? I have been looking for either some code or a plugin that will allow me to basically duplicate that feature in the backend only. Anyone know of a solution?

@manishjindal2020
Copy link

my question --how to add additional optional fee on checkd box in woocommerce checkout

@hemnathmouli
Copy link

Works perfectly.!

@bangbang1409
Copy link

how to add an extra fee with multi options (select dropdown)

@fordg
Copy link

fordg commented Oct 13, 2016

Work perfectly, but I have a problem if in cart page can display a result but not in checkout

function woo_add_cart_fee() {
    global $woocommerce;

    $woocommerce->cart->add_fee( __($_POST['item_name'], 'woocommerce'), $_POST['amount'] );
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

In chekout page, amout has zero result and item_name null, how to fixed it?

@amjad
Copy link

amjad commented Feb 12, 2017

Great, that works. Now how do you remove this added fee using another function?

@samtajoshi20
Copy link

I want this fees to be applied for one time per user, It should not be applied to returning customers again and again.
How can this be achieved?

@rehanhaider11
Copy link

Can we add extra fees per product ?

@deyan4
Copy link

deyan4 commented Sep 6, 2017

@bangbang1409, have you found out how to add a select option?

Thank you!

@baker13
Copy link

baker13 commented Sep 26, 2017

Hi guys I used this code and it worked 100% for me. but I want to ask does anyone know how I can add a coupon for this custom fee?

@asadaly111
Copy link

Working 100% :) Thanks buddy

@Darshan789
Copy link

Hey, I have a problem to add the fees using ajax on cart page.
On my cart I have added one checkbox. If checkbox checked then the fee add to the cart and updated the cart totals.
But right now, its' added to cart but on checkout the fees removed.
Can you please help for this?

check this link: http://www.simplepickle.com/

Thank you.

@pooju07
Copy link

pooju07 commented Sep 19, 2018

want to add donation checkbox in cart page this is working perfectly finr bt i want checkbox and accordingly price should be updated

@sanderlt
Copy link

how to make to choose value option from select list? :)

@mcpat1993
Copy link

I got this working so that the fee is properly assessed and the customer pays the new total including the fee. I actually want to pass that fee on to the vendor. How would I go about adding that to the total they are able to withdraw?

@vp956009
Copy link

vp956009 commented Jun 8, 2021

Extra Cost For Woocommerce you can use this plugin that is good for it

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