Want to make sure only registered customers can buy from your store? You can use a simple WooCommerce snippet to restrict purchases to logged-in users, giving you more control over who can place an order.
In this guide, you’ll learn how the snippet works and how to add it to your site. It’s a straightforward way to restrict WooCommerce purchases to logged in users while keeping the shopping experience clear for your customers.
Make WooCommerce restrict purchases to logged-in users with a simple snippet
Customers may be happy to browse before deciding whether to create an account—but the checkout is where you can set a clear boundary. This snippet lets guests shop and add items to their cart while requiring them to sign in before completing an order.
First, here’s what changes for a visitor who hasn’t logged in. Then you’ll see how to add the code in a way that’s easy to manage and undo.
What the snippet changes for guest shoppers
Rather than hiding products or blocking the cart, the code checks whether a visitor is on the checkout page. If they’re not logged in, it displays a notice and sends them to your account page, where they can sign in or register.
This approach keeps browsing open while making the purchase requirement clear at the right moment. It also allows customers to complete existing order-payment and order-received steps, so the restriction is focused on starting a new checkout.
addaction( 'templateredirect', 'mystorerequireloginforcheckout' );
function mystorerequireloginforcheckout() {
if (
ischeckout()
&& ! isuserloggedin()
&& ! iswcendpointurl( 'order-pay' )
&& ! iswcendpointurl( 'order-received' )
) {
wcaddnotice(
'Please log in or create an account to complete your purchase.',
'notice'
);
wpsaferedirect( wcgetpage_permalink( 'myaccount' ) );
exit;
}
}For registration to be available, enable account creation during checkout or on the “My Account” page in WooCommerce’s account settings. You can adjust the notice text to match your store’s voice.
Once you understand the checkout behavior, choose a safe place for the snippet. The installation method matters: adding code to a parent theme can mean losing it when that theme updates.
Add the code safely to your site
For most store owners, a snippets manager is a convenient option because it keeps custom code separate from theme files. WordPress’s Code Snippets plugin is one example; a child theme’s functions.php file is another option if you’re comfortable editing theme code.
- Back up your site or test the change on a staging copy first.
- Paste the snippet without adding another
<?phpopening tag in a snippets manager. - Save and activate it, then test checkout in a private browser window while logged out.
Confirm that guests are redirected, logged-in customers can proceed, and existing order links still work. If anything behaves unexpectedly, deactivate the snippet and review your checkout or account-page settings.
Set up and test the login-only purchase rule
A small change at checkout can still affect how customers move through your store. Before inviting shoppers to use the new flow, decide what message will greet guests and check the journey from browsing to placing an order.
Start with the customer-facing details, then test the key pages in both logged-in and logged-out sessions. This helps catch confusing redirects or conflicts with other checkout features.
The notice is often a guest’s first clue that an account is required. Consider how its wording and destination will help them take the next step.
Choose what guests see when they try to buy
The snippet displays a notice before sending a logged-out visitor to your “My Account” page. Keep the message specific and helpful—for example, “Please log in or create an account to complete your purchase.” If registration is enabled, saying they can create an account makes the requirement feel less like a dead end.
Check that the account page gives guests clear options to sign in or register, and that the return path after login is understandable. Depending on your store’s setup, they may need to return to their cart and begin checkout again. If you use a custom account page or login plugin, confirm that the redirect still lands on the right place and doesn’t send customers in a loop.
Also consider whether the wording fits your checkout design. A concise notice works well on most stores; avoid promising that a guest’s cart will be preserved unless you’ve tested that behavior.
With the message and destination in place, test how the rule behaves across the full shopping journey. A few checks in a private browser window can reveal issues that aren’t visible while you’re signed in as an administrator.
Test product pages, the cart, and checkout
Open a private or incognito window so you can test as a logged-out shopper. Follow the same steps a customer would, and repeat them after signing in to make sure the restriction applies only to guests.
- Product pages: Confirm guests can browse products and use the usual add-to-cart controls.
- Cart: Check whether guests can view and update their cart, and note whether the cart remains available after the redirect.
- Checkout: Verify that a guest sees the notice and reaches the account page, while a logged-in customer can proceed.
- Order links: If your store uses payment or order-confirmation links, check that those steps still work as intended.
Finally, test any express-checkout buttons, subscriptions, or plugins that create a direct route to checkout. These can change the usual path through your store, so confirm they respect the login requirement too.
Troubleshoot WooCommerce restrictions and improve the shopper experience
A redirect that works on your test browser may behave differently when a cache, theme, or extra checkout feature gets involved. If shoppers report trouble, check the full route from checkout to account access before changing the snippet.
Once the restriction is behaving consistently, a few small account-page improvements can make the required sign-in feel like a clear next step rather than a barrier.
Check for theme, plugin, and caching conflicts
If guests aren’t redirected—or logged-in customers are—look for another feature affecting checkout. Testing one change at a time on a staging site can help you identify the cause without disrupting live orders.
- Review checkout-related plugins: Temporarily disable suspected plugins on staging, such as custom checkout, login, or redirect tools, then test again.
- Check your theme: Switch briefly to a standard theme on staging to see whether a theme customization is interfering with the redirect or notice.
- Clear caches: Purge your site, hosting, and CDN caches, then test in a private browser window. Exclude checkout, cart, and account pages from caching where your cache provider allows it.
Use WooCommerce’s self-service troubleshooting guide to help narrow down extension or theme conflicts. If you use a redirect or security plugin, check its rules too; conflicting redirects can send visitors in a loop. Keep a note of each change, and restore any plugins or settings you tested once you’ve found the cause.
After the technical checks, consider what happens when a guest reaches the account page. A smooth sign-in or registration step can reduce confusion and help customers continue with confidence.
Make it easy for guests to log in or create an account
Give visitors a clear choice between signing in and registering. In WooCommerce’s account settings, confirm that account creation is enabled on the “My Account” page if you want new customers to be able to register there.
Make the next step obvious with concise labels and a brief explanation of why an account is needed. For example, you could add: “Sign in or create an account to continue to checkout.” Avoid suggesting that a cart will definitely be saved unless you’ve confirmed that behavior on your store.
Finally, test the complete journey with a new account and an existing one. Check that login succeeds, the customer can return to shopping, and the account page works well on mobile. If your login tool supports a return destination, configure and test it carefully so customers can resume checkout without being sent to an unexpected page.
Keep checkout exclusive while making the next step clear
A login requirement gives you a simple way to control who can complete purchases without closing your store to casual browsers. With the snippet in place, guests can explore products, while a clear sign-in or registration path helps them understand how to continue.
Add the code in a maintainable way, then test the full shopping journey—including account access, checkout, and any direct payment links. If something goes wrong, check for conflicts with your theme, plugins, or caching before adjusting the rule.
With a little testing and a helpful account experience, you can restrict WooCommerce purchases to logged-in users while keeping checkout straightforward for customers who are ready to buy.