Woocommerce, Woocommerce CRM

WooCommerce Automatically Log Out Customers After Checkout

WooCommerce log out customers after checkout

In some WooCommerce stores, keeping WooCommerce log out customers after checkout is unnecessary. This is especially true for guest-style purchasing experiences, temporary customer accounts, wholesale portals, or stores where buyers rarely return after placing an order.

At the same time, logging users out immediately after payment can create problems. If the logout happens too early, customers may lose access to their Thank You page before they can review order details, download files, or confirm their purchase information.

A better solution is to delay the logout until after the Thank You page has been displayed. This creates a smoother customer experience while still automatically ending the session once the customer continues browsing the site.

In this tutorial, you’ll learn how to automatically log users out after WooCommerce checkout using a lightweight PHP snippet. The solution works by setting a temporary WooCommerce session flag after payment and silently logging the customer out during their next page visit.

What This WooCommerce Snippet Does

  • Detects successful WooCommerce checkouts
  • Allows customers to fully access the Thank You page
  • Stores a temporary logout flag in the WooCommerce session
  • Automatically logs out users on their next page load
  • Prevents repeated logout actions
  • Keeps the checkout flow smooth and uninterrupted

WooCommerce PHP Snippet: Log Out Customers After Checkout

The following snippet safely logs customers out after checkout completion without interrupting the order confirmation page.

1. Set a Logout Session Flag After Checkout

This first function runs when the WooCommerce Thank You page loads. Instead of logging the customer out immediately, it stores a temporary session value that will trigger the logout later.

/**
 * Store Logout Trigger After WooCommerce Checkout
 */
add_action( 'woocommerce_thankyou', 'custom_wc_store_logout_session_after_order' );
function custom_wc_store_logout_session_after_order( $order_id ) {
    if ( ! is_user_logged_in() ) {
        return;
    }
    if ( ! WC()->session ) {
        return;
    }
    WC()->session->set(
        'custom_wc_pending_logout',
        'yes'
    );
}

2. Automatically Log Out the Customer on the Next Page Visit

This second function checks for the session flag during future page loads. If the customer is logged in and the logout flag exists, WooCommerce logs the customer out and removes the session value.

add_action( 'template_redirect', 'custom_wc_auto_logout_after_order' );
function custom_wc_auto_logout_after_order() {
    if ( ! is_user_logged_in() ) {
        return;
    }
    if ( ! WC()->session ) {
        return;
    }
    $logout_pending = WC()->session->get(
        'custom_wc_pending_logout'
    );
    if ( ! $logout_pending ) {
        return;
    }
    WC()->session->__unset(
        'custom_wc_pending_logout'
    );
    wp_logout();
}

How This Works

Once the WooCommerce checkout process is completed, the snippet stores a temporary session variable instead of forcing an immediate logout. This ensures the customer can still view the Thank You page and order details normally.

When the customer visits another page afterward, WooCommerce detects the stored session flag and silently logs the user out in the background. The session value is then removed so the logout only happens once.

This method avoids disrupting the checkout confirmation process while still automatically ending customer sessions after purchase completion.

Where Should You Add This Code?

Add the PHP snippet to your child theme’s functions.php file or use a custom code snippets plugin.

Before testing, temporarily switch to the Storefront theme and disable non-essential plugins to rule out compatibility issues.

Explore More Blogs : WooCommerce Refund Request Button on My Account Page