Here is a quick but useful snippet to automatically add a custom fee to the WooCommerce cart. This snippet applies for each user role a defined flat fee to the cart using the default currency. Simply place this code within the functions.php file of your theme folder:
Add Custom Fee to WooCommerce Cart dynamically
74 responses to “Add Custom Fee to WooCommerce Cart dynamically”
-
HI,
This is not working for me, and i need it damm urgently..
Can you help me?
I added this to woocommerce function.php file. And even tried adding in wordpress function.php..
But nothing get added to cart.. :(-
make sure you added the code to functions.php within the theme field, that should work.
-
Sir,
“within the theme field” means?-
i mean the theme folder
-
-
Also, i am using wordpress and installed woocommerce as a plugin.
-
Hello Remi
My issue is different from others.
all things works perfectly only issue i added your cart fee code fee showing in checkout but extra fee not included when i placed order take old price.
-
-
-
Okay, so it shows only at “Cart” but not at “checkout”
Also, it does not update the cart total.-
Good catch, i need to update the code!
-
Hey, pls update the code asap :).
Can you update this thing so that it add the price to the total as well as to show it on checkout page.
When can i see the updated code?
-
-
-
Heya,
When i can see the updated code?
-
Updated!
-
-
Is there any progress on this? it is exactly what I am looking for… need this urgently
-
i’ll try to do it asap, i’m pretty busy at the moment
-
Updated!
-
-
Thanks Remi!, Quick question… can you have it so it only affects some of the products… maybe add specific product ID’s to code?
-
yes this could be done but it would require much more code! ;-)
-
-
any Idea? how to make it product specific? anyone?
-
If I replace “5” with a variable, is that possible?
-
Yes!Got an answer already!
Thanks for the snippet!!!
-
-
Hi,
Realy like the code. Just what i needed.
But I like to add a checkbox so the user can choose if they want to add the extra cost to there order. Any idea how i can do that?Thanks,
Tessa-
in that case you can dynamically add the extra cost depending on a $_REQUEST value.
-
What exactly I need to add to your code when you say to add a $_REQUEST value?
Thanks-
you need to check if a post or get var is sent
-
Is this something either of you were able to figure out? I am trying to do something similar, and I am very close, but I can’t seem to get my function to actually check the post values.
-
-
-
-
-
Thanks, it’s working for me both checkout and cart.
-
Awesome!
-
Hi Remi
I am using your code for add the custom fee .
It is working fine on the cart page.
It is also appearing on the checkout page too. But it resets to the original cart total in few seconds automatically, any why it is doing so.Thanks in advance
-
-
-
how to i add custom field in admin for each product.
-
you can use the WooCommerce Product Addons extension for that.
-
-
it’s a paid plugin. any other free plugin suggest me.
-
Hi Remi,
Its a nice piece of code.
I need the same code to subtract an amount or a randon amount from the sub-total in woo commerce.Can you please help me out in this.
Its very urgent for me.
Regards,
Snigdha-
In that case, the best thing is to dynamically use a coupon: http://stackoverflow.com/questions/15744689/how-do-i-programmatically-apply-a-coupon-in-woocommerce
-
-
Hi. Is there a way to have the custom handling price applied to only a select products or array of products?
-
Yes you could do it if you add a statement that loop through the cart content and check for product IDs in the cart. Then add the fee if the IDs are in the loop. Does that make sense?
-
Hi remi great site – just found it! I’m a newbie, so for adding specific fee in check for specific ID, what kind of loop statement do I use? In context: when customer selects 1 color print job from variations, he gets charged $35 setup fee, but if he selects 2 color, he should get charged $70 setup fee, in addition to fee he’ll be paying for the actual product order (based on qty). Thanks in advance
-
-
-
Hi, this is a great piece of code. Is it possible to make this a percentage of the cart total instead of a fixed amount?
-
Thank you. Not only did this snippet allow me to finish and deploy my clients ecommerce store; it also gave me the much needed introduction to filters and actions. Next I am going to check out your posts on security. Very cool.
-
Awesome, i’m glad i could help you!
-
-
Hi, Great snippet!
I would like to add an option for expedited shipping on my e-commerce website. I want to have a checkbox which the customer can choose if they want to add the extra cost (as expedited shipping which would help get their orders fast ) to there order.
and on updating page the total amount for the cart should get updated. I want those extra fields to get displayed on cart page (besides individual product having a checkbox) & on cart page also.
Any help would be appreciated. Thanks in advance -
Hi, thank you for the code. Is it possible to set multiple fees based on the subtotal. For example if the subtotal was > 10$ the fee would be 2$ and if the subtotal was <= 10 then the fee would be 5$ etc. Thank you.
-
Hi Remi, I have a retaurant website and i would like the customers to add a tip when they order and pay online. Is they are anyway I can do it? I’m using WooCommerce Plugin on WordPress.
Thanks
-
I’m wondering the same thing. I want the customer to add the fee (tip) at checkout. Would this be complicated Remi? Thanks for the snippet though!
-
-
Hi there,
Very useful bit of code, I have used this to replace a plugin as it takes into account any shipping costs.
With the plugin that I was using you could add different fees dependant on the payment gateway chose, how can I hook into that to only add this fee for a certain gateway?
Thanks-
That would require some more custom code using AJAX i guess, it’s a bit too far from the post content ;-)
-
-
Hi Remi,
I’d like to add some custom text fields for users to input some necessary message like the following and have them displayed on the order list in admin dashboard:
Item Price Quantity Total Notes
———————————————————————————————————–
Product A $10 1 $10 “custom text fields”
Product B $5 1 $5 “custom text fields”
Product C $15 1 $15 “custom text fields”Can you pls advise how I can make it? Or can I hire you to do it?
Thanks a lot!!!
-
HI Remi
Love your work, really helpful.
I’m wondering how I coudl modify yoru code snippet to add a fixed fee to *each* product in the cart. My situation – the site is selling courses, each one with a price, plus fixed enrollment fee for each one.
So I’d like to have my cart show something like:
Course 1: Total Price: $ 3,000
Enrollment Fee: $ 80
Course 2: Total Price: $ 1,500
Enrollment Fee: $ 80
Total: $ 4,660
I.e add an extra charge to each item, and include these in the cart final total…
If this is feasible, can you give me a hint please?
Thanks in advance!
Ed
-
Hi Remi,
I’d like to add some custom text fields for users to input some necessary message like the following and have them displayed on the order list in admin dashboard:
Item Price Quantity Total Notes
———————————————————————————————————–
Product A $10 1 $10 “custom text fields”
Product B $5 1 $5 “custom text fields”
Product C $15 1 $15 “custom text fields”Can you pls advise how I can make it? Or can I hire you to do it?
Thanks a lot!!!
-
Remi,
Thanks for the code. It works great on my site to add a static fee to checkout. However, when a user enters a coupon on the cart page, the page refreshes showing a double charge of the fee added with this snippet. This is corrected on the checkout page, yet is still confusing for customers on the cart page. Any suggestions about how to troubleshoot?
-
Hi Remi,
Just want to ask, if it is possible to have different handling fee depending on the amount of cost of products purchased?
e.g.
$1 – 200 ( $10 shipping fee + $5 handling fee )
$201 – 400 ( $10 shipping fee + 0 handling fee )
$401 – above ( free shipping and 0 handling fee )is this possible?
Thank you!!!
-
Hi Remi, this is great snippet. Thank you, it’s really helpful.
I would like to seek your advice if I want to use two variable to replace the ‘5’.
I have two Shipping Classes, A and B. If all products in a Cart have Shipping Class A, then the charge is 5, however, if at least one product has Shipping Class B or all products have Shipping Class B, the charge is 10.
Thanks a bunch -
Hi Remi,
This custom fee is not reflecting in order totals neither on checkout page. Can you please recheck the snippet ?
-Shishir
-
Hi Remi,
I found the solution to my problem and I think of others too.You need to change the action hook from
“woocommerce_before_calculate_totals”
to
“woocommerce_cart_calculate_fees”This will update the order totals and will work on checkout page too.
-
Shishir,
Great find! That fixed the “double charge” error for me after using coupons.
-
Updated ;-)
-
-
-
Hi Shishir,
Thank you for mentioning the hook change. It helped us fix an issue in our plugin Order delivery date for WooCommerce (Plugin Link) that had been caused by the Woocommerce 2.2.x upgrade.
Pinal
-
Hi Remi,
This is exactly what I need however once I’ve added this code into my child theme functions.php file, it is giving me the dreaded white screen? Does it need to be in the ACTUAL theme functions file and not the child theme one?
Not sure if that makes a difference?Thanks
-
Hey Remi, I need exactly what you have here, but with a condition of only showing when the subtotal is $99.99 and below.
-
I want to add total weight before total, can you help me ?
this code i used but not placed on tabel cart total
// Store cart weight in the database
add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');function myprefix_cart_extra_info() {
global $woocommerce;
echo '';
echo '' . __('Total Weight:', 'woocommerce');
echo ' ' . $woocommerce->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit');
echo '';
echo '';
}
-
Hi,
I’ve used your code to out put after calculate shipping cost .The calculation is done throgh an API which I added a script in cart.php.Then I stored the value in a session ,and in functions.php I assigned that session value in a variable inside your function.
$extra=$session[‘ship_cost’];
$woocommerce->cart->add_fee( __(‘Custom’, ‘woocommerce’), $extra);The issue fou d out was as I created the session in cart.php ,your function trigger before cart.php.So the session variable has the old value.Once I reload the browser seems getting the latest value.
Please help me with this -
This looks great! How would I call a value in an ‘if’ statement to check a custom option to add a fee? Like if for some reason I either had to avoid using variations, or add that on to variations.
-
You can do something like :
function woo_add_cart_fee() {global $woocommerce;
if( get_option('myfield') == 'value' ) { $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 ); }
}
-
Thanks! I’ll get right to trying this.
-
-
-
Awesome piece of code Remi. Can you think of a simple way to add a condition for cart quantity. Only add the fee if the quantity is X? I’ve tried everything I can think of, but getting the cart quantity is crazy weird.
-
Hi rexfrancis, I’m doing that exactly, it’s very simple:
//Discount a value for quantity products in your cart
function woo_add_cart_fee() {
global $woocommerce;//Discount for quantity
$quantity_products = $woocommerce->cart->get_cart_contents_count();
$discount_value = ($quantity_products – 1) * 5;$woocommerce->cart->add_fee( __(‘Discount for quantity’, ‘woocommerce’), -$valor_a_descontar );
}
add_action( ‘woocommerce_cart_calculate_fees’, ‘woo_add_cart_fee’ );Thanks Remi, I was doing the discount using coupons but this its too much simple! :)
-
(Fixed variable spanish name ;)
Hi rexfrancis, I’m doing that exactly, it’s very simple:
//Discount a value for quantity products in your cart
function woo_add_cart_fee() {
global $woocommerce;//Discount for quantity
$quantity_products = $woocommerce->cart->get_cart_contents_count();
$discount_value = ($quantity_products – 1) * 5;$woocommerce->cart->add_fee( __(‘Discount for quantity’, ‘woocommerce’), -$discount_value );
}
add_action( ‘woocommerce_cart_calculate_fees’, ‘woo_add_cart_fee’ );Thanks Remi, I was doing the discount using coupons but this its too much simple! :)
-
-
Hi Remi Corson,
I am using woocommerce. I want to add Coupon discount on cart total. Now its working for cart sub total. Can you please suggest me how is it possible.Thanks !
-
Hi,
Any luck with this? I am trying the same.
The problem that we are facing is, a 100% coupon if applied, it still goes to the payment gateway just for the processing fee.
So, the processing fee need to take care of the coupon as well.
-
-
Hi Remi,
Is there way to dynamically add extra cost to each product subtotal in cart page..?
I need to add FET(federal excise tax) for each product which is different for each product.
To store FET for each product I have created custom field in product page.
How can add these FET value to each product subtotal…?
-
Hey Rémi,
I would love the exact same plugin, but I would like to add 18% to the actual cart.
That is the commission for my service.It would need to appear on checkout and add to cart page.
Do you think that would be possible ?
Many thanks .
-
Hello all,
Nicely presented !
This plugin may solve the problem easy, it allows you to charge extra fees in cart, based on the combination of multiple conditional rules that you configure in your woocommerce store.
I hope it may help!
-
Hello,
Is anyone how i could to the same but adding a checkbox ?
If checkbox is checked it adds the fee to the total
Thanks -
I found this but i suppose it test if the checkbox is checked when the page is loading , this is not ajax…
http://www.affectivia.com/blog/have-a-checkbox-on-the-checkout-page-which-updates-the-order-totals/ -
Hey mate,
I am wondering, can a parameter be created so that this awesome code works with ONLY a specific rule?
Let’s say we have a specific set of products that have been discounted by 50%. The rule is if a single product is on sale for 50% only then the “CUSTOM” fee is added to the Woocommerce cart. Only when this rule is true will the CUSTOM fee activate and show up in the frontend.
Is this possible mate? Or do you have recommendations for a plugin or any solutions?
I wanted to add a special fee to only the discounted products that are discounted to a specific rate.
Leave a Reply
You must be logged in to post a comment.