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.

<?php
/**
 * Charge Custom Shipping Fee
 * Fee based on WC cart subtotal
 */
add_action('woocommerce_cart_calculate_fees','add_custom_shipping_fee');
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

  •  
  •  

Comment

2 Comments

  1. Reigel GallardePublished: 2 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.

  2. IftekharPublished: 2 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 to Iftekhar

Note: Convet HTML, PHP, JavaScripts from Postable, 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.