Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Make Shipping Method Fields Mandatory on WooCommerce Checkout Page?

By default, one shipping method is always preselected. Occasionally, customers may unintentionally place an order without choosing a specific shipping method. Would you like to display a notice to customers prompting them to choose a shipping method? This code enables you to implement such a notice.

// Validate shipping method fields and returning an error if none is choosed
add_action( 'woocommerce_checkout_process', 'ts_shipping_method_validation', 20 );
function ts_shipping_method_validation() {
    if ( ! isset( $_POST['shipping_method[0]'] ) ){
        wc_add_notice( __( "You need to choose any of your shipping option.", "woocommerce" ), 'error' );
    }
}

Output

When the customer doesn’t select any of the shipping methods and tries to place an order, will encounter an error message as shown below.

How to Make Shipping Method Fields Mandatory on WooCommerce Checkout Page?

In contrast to the above functionality, a store owner can also set one of your preferred shipping method as a preselected option always. Explore further here on how to set default shipping method in WooCommerce cart.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
2 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Paul
5 months ago

This looks excellent. But to work properly it presumable needs no shipping option to be preselected. How do I to stop WooCommerce preselecting an item please?

2
0
Would love your thoughts, please comment.x
()
x