WooCommerce: Automatically Log Out Customers After Checkout

In WooCommerce, there are cases where you might want to log out a user after they complete a purchase. For example, new customers, one-time buyers, or sites where accounts are only needed temporarily, keeping users logged in after checkout may not make sense.

However, logging them out too early — like immediately on the Checkout page, or when the Thank You page loads — can prevent them from seeing their order details.

The ideal solution is to defer the logout until the user navigates away from the Thank You page. This way, checkout completes normally, the order confirmation is visible, and the user is safely logged out on their next visit.

In this post, we’ll show a simple PHP snippet that achieves this using WooCommerce sessions. The code sets a logout flag after checkout and automatically logs the user out silently the next time they visit any page, keeping the process smooth and user-friendly.

Here’s how to implement it.

The WooCommerce Thank You page — customers get to see their order details before being logged out at the next page visit.

PHP Snippet: Log Out WooCommerce Users After Checkout Completion

This PHP snippet works in two steps to safely log out users after they complete a WooCommerce checkout.

The first step runs on the woocommerce_thankyou hook, which fires when the order is successfully processed and the Thank You page is displayed. Here, we set a session flag called logout_after_thankyou. This flag acts as a marker indicating that the user should be logged out the next time they visit any page on the site.

The second step runs on the template_redirect hook, which executes on every page load. The code checks if the user is logged in and if the logout_after_thankyou flag is set in their session.

If both conditions are met, the user is logged out using WordPress’ wp_logout() function, and the WooCommerce session is cleaned up with WC()->session->cleanup_session(). The flag is then removed to prevent the logout from happening repeatedly.

This approach ensures the user sees the Thank You page and their order details but is logged out automatically the next time they browse the site. It’s a smooth, user-friendly way to handle post-checkout logouts without disrupting the checkout flow.

/**
 * @snippet       Log Customers Out @ WooCommerce Thank You Page
 * @tutorial      https://businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 10
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_thankyou', 'bbloomer_set_logout_after_checkout' );

function bbloomer_set_logout_after_checkout( $order_id ) {
    if ( is_user_logged_in() ) {
        WC()->session->set( 'logout_after_thankyou', true );
    }
}

add_action( 'template_redirect', 'bbloomer_logout_after_thankyou_silent' );

function bbloomer_logout_after_thankyou_silent() {
    if ( is_user_logged_in() && WC()->session->get( 'logout_after_thankyou' ) ) {
        wp_logout();
        WC()->session->__unset( 'logout_after_thankyou' );
    }
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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