Add Custom Fee Based on Cart Subtotal

Perhaps one of the biggest headache for WooCommerce based eShop owner is that to determine the shipping cost settings on their site. Quite often they would be scratching their head around this issue. Now, luckily there are so many plugins out there that one should not take this issue too seriously.

Now, one easy solution could be charging the shipping cost based on the cart total or in other words total cost (excluding shipping, taxes etc.) of the items that the customer is ordering.

For the sake of explanation, lets think of about a hypothetical situation where you want to charge $10 shipping fee if the order total is $50 or less. If the order total is equal to $100 or less, you may want to charge $5 shipping. At the same time, if the order is more than $100, you may want to provide Free Shipping to your customer.

This is fairly a simple concept which may or may not fit your requirements but this is how you can do this without any plugin. This is how you can do it.

 * Charge Custom Shipping Fee
 * Fee based on WC cart subtotal
function add_custom_shipping_fee() {
   global $woocommerce;
   // get cart subtotal
   $st = WC()->cart->subtotal;
   if ($st <= 50) {
      $woocommerce->cart->add_fee(__('Shipping','woocommerce'), 10);
   } else if ($st <= 100) {
      $woocommerce->cart->add_fee(__('Shipping','woocommerce'), 5);
   } else {
      $woocommerce->cart->add_fee(__('Shipping','woocommerce'), 0);

All you need to do is to simply copy and paste this snippet on your theme’s functions.php page and update it. Rest should be done automatically when customer tries to check out. Here is a little Demonstration of this snippet.

WooCommerce Cart SubtotalWooCommerce Changing Cart Total.

What we are doing here is simply checking the cart total when customer tries to check out. If the subtotal (the price of the items only) is less than our desired sales amount(mentioned above), we are simply adding our custom shipping cost and only then calculating the grand total (including tax and stuff).

I hope you would find this snippet to be very useful. Let me know if you have any additional question or query on this regard. Thank you.

Today In History




  1. Giang LePublished: 2 years ago

    Can you guide me apply this extra few with shipping fee based on location?

    • Iftekhar BhuiyanPublished: 2 years ago

      Giang, I believe you should be able to do that easily from WooCommerce itself. Simply head towards the “shipping” tab from the WooCommerce “settings” option. You can create and customize zones and assign your desired regions/locations and methods. That should resolve your issue. Thanks.

  2. Reigel GallardePublished: 5 years ago

    Great idea! But this is really not a shipping fee, just a additional fee named as “Shipping”. This can’t calculate shipping that involves shipping class and other shipping factors like shipping zones.

    • Iftekhar BhuiyanPublished: 5 years ago

      Nailed it! I totally agree with you on that. Perhaps the title of this post was wrong and I finally decided to change it. My actual intent was to demonstrate how to add custom Fee based on cart subtotal. I am aware of the “Shipping Class” issue and hope to write on that issue as well in near future. Thank you very much for your valuable thoughts though.

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.