Removing Checkout Button When Shipping is Not Available

It’s easy to miss the small notification that appears when no shipping methods are available. This can cause confusion and frustration when the customer continues to the checkout page but cannot complete their order. To help avoid this bothersome situation, to may be a good idea to remove the ‘Proceed to Checkout’ button that appears beneath the subtotal on the cart page. Adding the following code snippet will add an extra check to WooCommerce and remove the button if no shipping options are found.
(WooCommerce 2.4+)

This also includes users who have enabled the multiple shipping packages feature available in WooCommerce 2.1+. It will check each package, and if any of them are missing shipping options, the button will be removed.

function disable_checkout_button_no_shipping() {
    $package_counts = array();
    
    // get shipping packages and their rate counts
    $packages = WC()->shipping->get_packages();
    foreach( $packages as $key => $pkg )
        $package_counts[ $key ] = count( $pkg[ 'rates' ] );
    // remove button if any packages are missing shipping options
    if( in_array( 0, $package_counts ) )
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );

Prevent Viewing the Checkout Page, too!

Furthermore, if you wish to prevent access to the checkout page when shipping is unavailable, this similar function should do the trick.

function prevent_checkout_access_no_shipping() {
    // Check that WC is enabled and loaded
    if( function_exists( 'is_checkout' ) && is_checkout() ) {
    
        // get shipping packages and their rate counts
        $packages = WC()->cart->get_shipping_packages();
        foreach( $packages as $key => $pkg ) {
            $calculate_shipping = WC()->shipping->calculate_shipping_for_package( $pkg );
            if( empty( $calculate_shipping['rates'] ) ) {
                wp_redirect( esc_url( WC()->cart->get_cart_url() ) );
                exit;
            }
        }
    }
}
add_action( 'wp', 'prevent_checkout_access_no_shipping' );
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.