Last updated on October 18, 2023

WooCommerce guest checkout not working

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Make sure WooCommerce guest checkout works smoothly.

The code snippet provided below can be used to fix the issue of WooCommerce guest checkout not working. This issue may occur when the guest checkout option is enabled in WooCommerce settings, but customers are still being prompted to create an account or log in during the checkout process.

To resolve this issue, you can add the following code to your theme’s functions.php file or create a custom plugin:

/**
 * Disable account creation for guest checkout in WooCommerce.
 */
function wpsnippets_disable_guest_checkout_account_creation() {
    remove_action( 'woocommerce_after_checkout_registration_form', 'woocommerce_registration_form' );
}
add_action( 'init', 'wpsnippets_disable_guest_checkout_account_creation' );

This code snippet hooks into the init action and removes the woocommerce_registration_form function from the woocommerce_after_checkout_registration_form action. By doing so, it disables the account creation form for guest checkout.

This code snippet can be useful in scenarios where you want to provide a seamless guest checkout experience for your customers without requiring them to create an account. It ensures that the guest checkout option works as intended and eliminates any confusion or inconvenience caused by unnecessary account creation prompts during the checkout process.

Examples

Example #1: Disabling Guest Checkout in WooCommerce

This example demonstrates how to disable the guest checkout functionality in WooCommerce by forcing customers to create an account before making a purchase.

function wpsnippets_disable_guest_checkout() {
    remove_action( 'woocommerce_after_checkout_form', 'woocommerce_checkout_login_form', 10 );
}
add_action( 'init', 'wpsnippets_disable_guest_checkout' );

The code snippet above uses the remove_action() function to remove the woocommerce_checkout_login_form action hook from the woocommerce_after_checkout_form hook. This action hook is responsible for displaying the guest checkout option on the checkout page. By removing this action, the guest checkout option is effectively disabled.

Example #2: Redirecting Guest Users to Login/Register Page

In this example, we redirect guest users to the login or registration page when they attempt to access the WooCommerce checkout page.

function wpsnippets_redirect_guest_checkout() {
    if ( ! is_user_logged_in() && is_checkout() ) {
        wp_redirect( wp_login_url() );
        exit;
    }
}
add_action( 'template_redirect', 'wpsnippets_redirect_guest_checkout' );

The code snippet checks if the user is not logged in and if the current page is the WooCommerce checkout page using the is_user_logged_in() and is_checkout() functions. If both conditions are met, the user is redirected to the login page using wp_redirect().

Example #3: Displaying Notice for Guest Checkout

This example shows how to display a notice on the WooCommerce checkout page to inform guest users that they need to create an account to proceed with the purchase.

function wpsnippets_display_guest_checkout_notice() {
    if ( ! is_user_logged_in() && is_checkout() ) {
        echo '<div class="woocommerce-info">Please create an account to proceed with the purchase.</div>';
    }
}
add_action( 'woocommerce_before_checkout_form', 'wpsnippets_display_guest_checkout_notice' );

The code snippet checks if the user is not logged in and if the current page is the WooCommerce checkout page using the is_user_logged_in() and is_checkout() functions. If both conditions are met, a notice is displayed using the echo statement. The notice is wrapped in a <div> element with the woocommerce-info class for styling purposes.

Last updated on October 18, 2023. Originally posted on October 29, 2023.

Leave a Reply

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