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.
Leave a Reply