How to Disable Checkout in WooCommerce When No Shipping Methods Are Available.

How to Disable Checkout in WooCommerce When No Shipping Methods Are Available
Share this post

This WooCommerce snippet enhances the checkout process by ensuring that customers can only proceed to checkout when valid shipping options are available. The code includes three key functionalities:

  1. Disable the Checkout Button:
    If no shipping methods are available for one or more shipping packages in the cart, the “Proceed to Checkout” button on the cart page is automatically disabled, preventing customers from advancing to the checkout page unnecessarily.
  2. Redirect Users from Checkout:
    If a customer attempts to bypass the cart and access the checkout page directly while no shipping methods are available, they are automatically redirected back to the cart page. This ensures a smooth user experience and prevents errors during checkout.
  3. Custom Message for No Shipping Options:
    A customized message is displayed to inform users that no shipping options are available. The message encourages users to double-check their shipping address or contact the store for assistance. This clear communication helps reduce confusion and improves user satisfaction.

Implementation Instructions

You can add this code to the functions.php file of your WordPress theme. To do so:

  1. Log in to your WordPress dashboard and navigate to Appearance > Theme File Editor.
  2. Open the functions.php file from your active theme.
  3. Copy and paste the code into the file and save your changes.
<?php
// Disable checkout if no shipping found
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);

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_get_cart_url()));
                exit;
            }
        }
    }
}
add_action('wp', 'prevent_checkout_access_no_shipping');

// Custom no shipping message
add_filter('woocommerce_no_shipping_available_html', 'my_custom_no_shipping_message');
add_filter('woocommerce_cart_no_shipping_available_html', 'my_custom_no_shipping_message');
function my_custom_no_shipping_message($message) {
    return __('There are no shipping methods available. Please double-check your address, or <strong><a href="/contact">contact us</a></strong> if you need any help.');
}

Compatibility

This code is designed for modern versions of WooCommerce (7.x and above) and adheres to WordPress best practices. It ensures smooth functionality across the cart and checkout processes, enhancing the overall user experience for your e-commerce store.

💡 Note: Test the code on a staging environment before applying it to your live site to ensure compatibility with your theme and plugins.

Leave a Reply

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