Require a WooCommerce account before adding to cart
Published Updated 2 min read
WooCommerce lets anyone add products to the cart and check out as a guest, and for most stores that is a sensible default. Competition sites, wholesale stores and members-only shops are different: they need to know who the customer is before buying starts, not after.
Turning off guest checkout does not get you there. It stops guests at the checkout page, after they have added items, browsed for several minutes and started typing an address. The place to stop a guest is the Add to Cart button itself.
Blocking at checkout is too late
With guest checkout disabled, a guest can still browse, fill a cart and get well into checkout before hitting the wall. On an ordinary shop that is minor friction. On a competition or prize draw site it costs entries: someone who has gone through several steps before learning they need an account often will not bother.
Replacing the button makes the requirement visible on the product page. The guest sees what is needed straight away, and the registration form is one click away instead of buried at the end of a checkout they cannot finish.
A notice where the button was
WooCommerce > Settings > Guest Restrictions has two settings: a switch for the restriction across the site, and the message guests see.
For guests, the Add to Cart button on product pages becomes a box with that message and links to the WooCommerce login and registration pages. Logged-in customers see the normal button and notice nothing. It works on simple, variable and grouped products through WooCommerce's standard hooks.
Closing the side doors
A hidden button is not enough on its own, because quick-add buttons and URLs like ?add-to-cart=123 add products without it. The plugin also hooks woocommerce_add_to_cart_validation, which runs on every add-to-cart request, rejects the guest's request with a wc_add_notice error and sends them to log in.
Competition sites and trade shops
Prize draws are the clearest case. The rules usually require verified entrants, and WooCommerce's default guest flow works against that. Blocking at Add to Cart means nobody enters without an account on record.
Wholesale stores that approve accounts before anyone can order get the first half here: a login before the cart is touched. A second small build can add the approval step, holding new registrations as pending until an admin approves them.
What you get
Included
- Admin settings page under WooCommerce > Settings for enabling the restriction and customising the guest message
- Replacement of the Add to Cart button with a styled notice and Login and Register links for guests
- Validation hook that blocks programmatic add-to-cart attempts by guests with a wc_add_notice error
- Works on simple, variable, and grouped WooCommerce product types
Not included
- Per-product control: the restriction applies to all products or none
- Role-based access: any logged-in user can add to cart regardless of their role
- Restricting product page visibility to logged-in users only
- Automatic redirect back to the original product page after a guest logs in
- Account approval workflow for new registrations
WP Force Login does the things on the right. This build covers the part most sites use.
Questions
Doesn't WooCommerce already have a setting to require an account?
It does, under WooCommerce > Accounts & Privacy. That stops guests completing a purchase, but leaves the Add to Cart button working, so guests can still fill a cart. This plugin stops them earlier, on the product page.
Can some products skip the restriction?
Not in this build; it applies to every product or none. A follow-up message can add a checkbox on each product's edit screen to opt it out.
Do guests land back on the product after logging in?
No. They land on the standard WooCommerce account page. Sending them back to the product they came from needs a second small build.
Build it in Steem
The button opens the builder with this guide's prompt filled in, so you can change anything before it runs. Nothing is built, and nothing is charged, until you send it.
Read the prompt (140 words)
Build a WordPress plugin for WooCommerce that prevents guests from adding products to the cart. Add a settings page under WooCommerce > Settings > Guest Restrictions with two fields: an enable or disable toggle and a custom message field with the default text 'Please create an account or log in to add products to your cart'. On all WooCommerce product pages, when the current visitor is not logged in, replace the Add to Cart button with a styled div containing the custom message and two inline links labeled 'Log in' and 'Create account' that point to the WooCommerce login and registration page URLs. Also hook into woocommerce_add_to_cart_validation to reject any guest add-to-cart requests that arrive through other means such as quick-add buttons or direct URL parameters, calling wc_add_notice with an error and returning false. Show nothing extra to logged-in customers.
Steem