Add Custom Fee to WooCommerce Cart dynamically

Remi | August 8, 2013 | in Tutorials

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

The author

About the author Remi: Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!

70 Comments

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

  2. 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. 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.

      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

  3. 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

    1. Remi Post author

      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

  4. 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.

  5. 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

  6. 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.

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

  8. 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

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

  10. 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

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

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

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

  14. 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

    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.

  15. 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

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

  17. 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

  18. 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. Remi Post author

      You can do something like :


      function woo_add_cart_fee() {

      global $woocommerce;

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

      }

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

  20. 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.

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

  22. 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 .

Leave a reply