Wooassist

Assistance for Your Woocommerce Store

  • How it Works
  • Pricing
  • Services
    • Site Maintenance
    • AI SEO and Content Marketing
  • Blog
    • How-To Articles
    • Code Snippets
    • SEO For E-Commerce
    • Theme and Plugin Reviews
    • Wooassist News
    • WordPress/WooCommerce News
    • Interviews
  • About Us
  • Contact
You are here: Home / Code Snippets / Disabling Shipping Methods Based on Order Subtotal: A Step-by-Step Guide for WooCommerce

Disabling Shipping Methods Based on Order Subtotal: A Step-by-Step Guide for WooCommerce

May 17, 2023 By Rob Leave a Comment

There are many scenarios where a store owner might want to disable certain shipping methods based on the order subtotal. For instance, they may have a minimum order amount required for certain shipping methods, such as free shipping or expedited shipping. Alternatively, they may want to restrict certain shipping methods for orders that don’t meet a certain threshold, such as only offering express shipping for orders over a certain amount.

By implementing the code outlined in this article, store owners can easily disable or hide certain shipping methods based on the order subtotal, ensuring that their customers are only offered shipping options that are appropriate for their order. This can help improve the overall customer experience and streamline the checkout process.

We will show you how to achieve this using some simple code snippets in WooCommerce.

Important Note: Paste the Code Snippets on Your Child Theme

If you’re making changes to the code of your WooCommerce store, it’s always a good idea to use a child theme. By adding the code to your child theme’s functions.php file, you can rest assured that your changes will be preserved even if you update your theme in the future. Plus, using a child theme can help prevent any conflicts that may arise from making changes directly to your site’s core files.

Step 1: Find the ID of the Shipping Method You Want to Disable

To get the ID of the shipping method, you can inspect the radio button using your browser’s dev tools. The value of the input element is your shipping method ID.

Step 2: Unset the Shipping Method

We will now need to create a function that will unset the target shipping method if the order total is less than the minimum amount. In this example, we will be disabling the Cash on Delivery shipping method if the order subtotal is less than $50.

In this case, the ID of the shipping method is ‘cod’. You will need to replace this value with the value of the shipping method you would like to hide.

You will also need to change the value after subtotal <. In This case, COD will be hidden if the total is below $50. Change this value to your desired amount.

Add the following code to your theme’s functions.php file:

function change_payment_gateway( $gateways ){
// Compare cart subtotal (without shipment fees)
if( WC()->cart->subtotal < 50 ){
// Then unset the 'cod' key (cod is the unique id of COD Gateway)
unset( $gateways['cod'] );
}
return $gateways;
}

Step 3: Display a Notice

The next step is to display a notice to the customer if the subtotal is below $50 and COD is selected as the payment method. We will create a function that will check the order subtotal and payment method, and display a message if necessary.

You will need to change the value after $minimum to your desired minimum amount.

You can also change the value “Minimum order for COD is %s please use another payment option.” ig you want to change the notice.

Add the following code to your theme’s functions.php file:

if ( ! function_exists( 'woa_cod_min_amt' ) ) :
function woa_cod_min_amt( $posted_data) {

global $woocommerce;
$minimum = 50; // set minimum order amount
$stotal = WC()->cart->subtotal;

// Parsing posted data on checkout
$post = array();
$vars = explode('&', $posted_data);
foreach ($vars as $k => $value){
$v = explode('=', urldecode($value));
$post[$v[0]] = $v[1];
}

$payment_method = $post['payment_method'];

// display message
if ($payment_method == "cod" && $stotal < $minimum) {
wc_add_notice(
sprintf( 'Minimum order for COD is %s please use another payment option.' , wc_price( $minimum ) ), 'error' );
add_filter( 'woocommerce_available_payment_gateways' , 'change_payment_gateway', 20, 1);
}
}

endif;

add_action('woocommerce_checkout_update_order_review', 'woa_cod_min_amt');

Step 4: Trigger the Function

The last step is to trigger the function every time the radio button of the shipping method is clicked. We will create a jQuery script that will track the event and trigger the function.

Add the following code to your theme’s functions.php file:

add_action( 'wp_footer', 'woa_custom_checkout_jqscript' );
function woa_custom_checkout_jqscript() {
if ( is_checkout()) :
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}

And that’s it! With these three code snippets, you can now disable a shipping method if the order total does not meet a certain amount.

Disabling shipping methods based on order subtotal is a powerful tool for any WooCommerce store owner who wants to streamline the checkout process and improve the customer experience. By implementing the code outlined in this article, store owners can ensure that their customers are only offered shipping options that are appropriate for their orders. Whether you need to set a minimum order amount for certain shipping methods or restrict certain methods for orders that don’t meet a certain threshold, this guide provides a clear and easy-to-follow solution.

People that read this article also liked

How to Make WooCommerce Pages Full Width in Storefront Theme sticky-header-featureHow to Make a Sticky Header in Storefront Theme How to Center Logo in Storefront Theme_featureHow to Center Logo in Storefront Theme 11 Things You Can Do to Increase the Security of Your WooCommerce Store

Filed Under: Code Snippets

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Woocommerce Support

wooassist-team-in-Iloilo

Get 2 Hours of
FREE SUPPORT

We are so confident that you will love our services that we will give you your first 4 hours at a 50% discount

That's 4 hours for only $75

BUY NOW

Happy Wooassist Customers

Awesome! Fantastic! I just went to our site and I see that our site has been fixed!! We are getting orders again thanks to you all at WooAssist! Our site has not worked and looked this good for many months!! You all are awesome! Thank you so much for fixing our site! You have a customer for life! Thanks for making my day wonderful!

Kenneth Arnold

We have been quite pleased working with WooAssist as they help fill in the gaps of our development needs, all at a great price. They are always timely and communicate great, I highly recommend their services.

James Grasty

My husband and I am EXTREMELY pleased with the WooAssist Team. They provide excellent service, are very knowledgeable, and super easy to communicate with. The team ALWAYS has our company's best interests in mind. I love WooAssist! All of you make my job easier.

Jennifer Taft leetaft.com

Categories

  • Code Snippets
  • How-To Articles
  • Interviews
  • SEO For E-Commerce
  • Theme and Plugin Reviews
  • Uncategorized
  • Wooassist News
  • WordPress/WooCommerce News

Recent Posts

  • How to Adapt Your Content Marketing Strategy in the Age of AI
  • Content Marketing for WooCommerce Stores: Are You Doing it Right (If You’re Doing it At All)
  • The Importance of a Content Marketing Strategy for WooCommerce Success
  • WooCommerce Site Speed Optimization: How to Disable Lazy Loading for Images Above the Fold
  • Automating Blog Posting with AI-Generated Content for Your WooCommerce Store
Let us support your online store so you can manage your business

Get started today

Get 2 Hours of FREE SUPPORT

We are so confident that you will love our services that we will give you your first 4 hours at a 50% discount

That's 4 hours for only $75

BUY NOW

Free eBook

Your subscription could not be saved. Please try again.
Your subscription has been successful.

YOURS FREE!

5 Things Every Online Store Can Fix On Their Website In The Next Week To Increase Sales

Quick Links

  • How it Works
  • Pricing
  • Blog
  • Contact
  • About Wooassist
  • My Account
  • Checkout
  • Privacy Policy
  • Cookie Policy
  • Terms and Conditions

Wooassist

Australia:
8 Storie St.
CLONTARF QLD 4019

Philippines:
San Miguel St.
Poblacion, Iligan City 9200

Connect

  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Copyright © 2023 ยท Wooassist

Yours FREE!

5 Things Every Online Store Can Fix On Their Website In The Next Week To Increase Sales