Add Custom Fee to WooCommerce Cart dynamically

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:

woocommerce-add-cart-custom-fee

74 responses to “Add Custom Fee to WooCommerce Cart dynamically”

  1. Pushkar

    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.. :(

    1. make sure you added the code to functions.php within the theme field, that should work.

      1. Pushkar

        Sir,
        “within the theme field” means?

        1. i mean the theme folder

      2. Pushkar

        Also, i am using wordpress and installed woocommerce as a plugin.

      3. graghuwanshi

        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.

  2. Pushkar

    Okay, so it shows only at “Cart” but not at “checkout”
    Also, it does not update the cart total.

    1. Good catch, i need to update the code!

      1. Pushkar

        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?

  3. Pushkar

    Heya,

    When i can see the updated code?

  4. Michael

    Is there any progress on this? it is exactly what I am looking for… need this urgently

    1. i’ll try to do it asap, i’m pretty busy at the moment

  5. Michael

    Thanks Remi!, Quick question… can you have it so it only affects some of the products… maybe add specific product ID’s to code?

    1. yes this could be done but it would require much more code! ;-)

  6. Michael

    any Idea? how to make it product specific? anyone?

  7. Eduardo

    If I replace “5” with a variable, is that possible?

    1. Eduardo

      Yes!Got an answer already!
      Thanks for the snippet!!!

  8. Tessa

    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

    1. in that case you can dynamically add the extra cost depending on a $_REQUEST value.

      1. soldark

        What exactly I need to add to your code when you say to add a $_REQUEST value?
        Thanks

        1. you need to check if a post or get var is sent

          1. tabytharourke

            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.

  9. Radhey Shyam Sharma

    Thanks, it’s working for me both checkout and cart.

      1. ravish

        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

  10. Radhey Shyam Sharma

    how to i add custom field in admin for each product.

    1. you can use the WooCommerce Product Addons extension for that.

  11. Radhey Shyam Sharma

    it’s a paid plugin. any other free plugin suggest me.

  12. snigdha

    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

  13. Vic

    Hi. Is there a way to have the custom handling price applied to only a select products or array of products?

    1. 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?

      1. anitorosyan

        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

  14. Scott Henning

    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?

  15. pathfinder

    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.

    1. Awesome, i’m glad i could help you!

  16. Aniket

    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

  17. JamesT

    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.

  18. Nishan

    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

    1. gbiondo2

      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!

  19. dominic.talbot

    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

    1. That would require some more custom code using AJAX i guess, it’s a bit too far from the post content ;-)

  20. janestone08

    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!!!

  21. edg

    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

  22. janestone08

    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!!!

  23. grantdailey

    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?

  24. mclordgt

    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!!!

  25. famo

    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

  26. shishirumrao42@gmail.com

    Hi Remi,

    This custom fee is not reflecting in order totals neither on checkout page. Can you please recheck the snippet ?

    -Shishir

    1. shishirumrao42@gmail.com

      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.

      1. grantdailey

        Shishir,

        Great find! That fixed the “double charge” error for me after using coupons.

  27. Pinal

    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

  28. saralt

    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

  29. aedmundson

    Hey Remi, I need exactly what you have here, but with a condition of only showing when the subtotal is $99.99 and below.

  30. badaionly

    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 '';
    }

  31. Nirmal

    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

  32. gpii

    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.

    1. You can do something like :


      function woo_add_cart_fee() {

      global $woocommerce;

      if( get_option('myfield') == 'value' ) { $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 ); }

      }

      1. gpii

        Thanks! I’ll get right to trying this.

  33. rexfrancis

    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.

    1. etilico1981

      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! :)

    2. etilico1981

      (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! :)

  34. masood

    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 !

    1. geniustaha

      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.

  35. skanda

    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…?

  36. bgas

    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 .

  37. tirth999

    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!

    WooCommerce Conditional Product Fees For Checkout

  38. pipo

    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

  39. pipo

    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/

  40. altshoppe

    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