Restrict Purchases by Customer and Country
Some products aren’t for everyone. A members-only download should go to members, a launch discount product should sell once per customer, and a licensed ebook or a regulated item may only be sold in certain countries. WooCommerce sells every product to anyone who reaches the cart, so stores end up refunding orders they never wanted.
Purchase Rules decides who can buy a product and where it can be sold. Limit a product to user roles, FluentCRM tags or WP Fusion tags, or to one purchase per customer. Lock products to countries by the visitor’s location, the billing country or the shipping country, for the whole store or product by product. Shoppers who don’t qualify get a clear reason, and checkout won’t go through.
Turn On Purchase Rules
- Open Stackless → Dashboard and turn on Purchase Rules in the Access & licensing section.
- For a store-wide country lock and the shopper messages, open Stackless → Settings → Purchase Rules.
- For a rule on one product, edit the product and open Product data → Purchase rules.
Nothing is limited until you set a rule. The Purchase rules tab isn’t shown for grouped and external products.
Choose Who Can Buy a Product
The Who can buy part of the product tab holds these fields:
- Roles: WordPress roles that may buy, such as Customer or a membership role.
- FluentCRM tags: shown when FluentCRM is active and has tags. The tags of the contact linked to the shopper’s account count.
- WP Fusion tags: shown when WP Fusion is active. Tags come from WP Fusion’s saved copy, with no call to your CRM.
- Buyer needs: Any of these (default) passes with one matching role or tag. All of these needs every role and tag you listed.
- Buy once: Each customer can buy this only once.
- Message: shown to signed-in buyers whose account doesn’t qualify, in place of the store’s Not allowed message. Leave it empty to use the store message.
Buy once looks at past paid orders, which in WooCommerce means Processing and Completed. An order counts when it belongs to the account, or when a guest order used the account’s email address. On a variable product it counts per variation.
Any who-can-buy rule needs an account, so a signed-out visitor is asked to log in first. That includes a rule with only Buy once ticked.
Each variation has its own Who can buy choice: Same as product (default), Own rule with the same fields, or Anyone can buy.
Lock Products to Countries
A country lock can come from the whole store, the product or a single variation. The nearest one wins and they never stack: a variation’s own rule replaces the product’s, and a product’s own rule replaces the store’s.
The Store-Wide Lock
Under Stackless → Settings → Purchase Rules, the Store-wide country lock section applies to every product:
- Rule: Sell only to these countries (default) or Sell everywhere except these countries.
- Countries: WooCommerce’s country list, with European Union (all member states) at the top for all 27 members. Leave it empty for no store-wide lock.
- Check: Visitor location (from your CDN’s country header), ticked by default, plus Billing country and Shipping country. Tick any mix.
- Message: optional, with the placeholders
{product}and{country}.
A Product or Variation Lock
On the product’s Purchase rules tab, Country lock has three choices:
- Store-wide rule (default): the product follows the store lock.
- Own rule: its own Rule, Countries, Check and Message, with nothing ticked under Check until you choose.
- No country lock: the product sells everywhere, whatever the store lock says.
Each variation adds Same as product (default), Own rule and No country lock. A rule with no countries or no Check box ticked locks nothing.
How the Visitor’s Country Is Found
Visitor location reads a country header that your CDN or server adds to each request. Nothing is looked up remotely. Set it up under Stackless → Settings → Purchase Rules in the Visitor location section:
- Detection: Automatic: CDN and server headers, then WooCommerce geolocation (default), Cloudflare header only, or My own header only.
- Custom header: a header your proxy sets to the two-letter country code, such as
X-Country-Code. Automatic mode checks it first. - Country unknown: Let them buy (default) or Treat as not allowed. It covers visitors with no country header, such as a site with no CDN, Tor or a local request. Address checks don’t use it.
Automatic mode reads the headers from Cloudflare, CloudFront, Vercel and App Engine, common proxy headers, and the nginx GeoIP2, mod_maxminddb and mod_geoip server modules. If none is there, it falls back to WooCommerce’s own geolocation, which works only when WooCommerce’s MaxMind database is set up.
The top of the Visitor location section shows the country found on your own request and where it came from. When a CDN is in front but sends no country, it tells you to turn on its country header (on Cloudflare, IP Geolocation).
The visitor’s country never changes the product page, so page caching keeps working. The lock applies when the shopper adds the product to the cart and again at checkout.
Billing and Shipping Checks
When the cart holds a product whose rule checks the billing or shipping country, the country dropdowns on the checkout offer only the countries every such product allows. Both the Checkout block and the classic checkout get the shorter list. If the store ships to all countries it sells to, the shipping list keeps countries that only a billing lock removed.
The list is only a convenience. The country is checked again when the order is placed, because saved addresses and express wallets can send any country. Orders from express payment buttons, such as Apple Pay, Google Pay and PayPal, get the same check. A product without a shipping address, such as a download, has its shipping check run on the billing country.
The Checkout Enhancements shorter checkout can hide the country field and fill in a saved country. While a product with a billing lock is in the cart, the country field stays visible, so the order is checked against the buyer’s real country.
If two locked products in the cart share no country, the lists stay full and the cart shows “The items in your cart can’t be bought from the same country. Remove one of:” followed by the product names. Checkout stays blocked until the shopper removes one.
What Shoppers See
A product the shopper can’t buy by role, tag or Buy once can’t be added to the cart. The product page hides the add to cart button and shows the reason in its place. For a variation, the reason shows when the shopper picks it.
A country lock refuses the add to cart with the reason as a notice. On the cart and checkout, each refused product gets its own error and the order can’t be placed. A product already in the cart that can no longer be bought, for example after the shopper logs out, stays in the cart with its reason, on the classic cart and checkout and on the Cart and Checkout blocks. Checkout stays blocked until the shopper removes it.
Edit the wording under Stackless → Settings → Purchase Rules in Shopper messages:
| Field | Default text |
|---|---|
| Login needed | “Log in to buy this.” The words link to the login page and come back to the product. |
| Not allowed | “This is not available to your account.” |
| Already bought | “You have already bought this.” |
| Visitor country not allowed | “{product}” is not available in your country. |
| Address country not allowed | “{product}” can’t be sold to {country}. |
| No country fits the cart | “The items in your cart can’t be bought from the same country. Remove one of: {products}.” |
{country} becomes the country’s name, and {products} a list of product names. An emptied field goes back to its default.
Some of these can be replaced by a rule’s own message:
- A product or variation’s Who can buy Message replaces Not allowed for signed-in buyers whose account doesn’t qualify.
- A country rule’s own Message, from the product, the variation or the store-wide lock, replaces both Visitor country not allowed and Address country not allowed.
- Login needed, Already bought and No country fits the cart always come from the settings.
Where Rules Apply
- The product page, the classic add to cart, AJAX add to cart and the Store API used by the blocks.
- The classic cart and checkout, and the Cart and Checkout blocks.
- Buy Links, Modal Checkout, On-page Checkout, Order Bumps and Saved Carts, which add to the cart through WooCommerce’s own checks.
- Post-purchase Offers: an offer whose product the buyer can’t buy isn’t shown, and a click is checked again.
These are never checked, so a rule can’t stop them:
- subscription renewals, including a renewal paid from its pay link
- orders you create in the admin or through the WooCommerce REST API
- invoices and quotes, which are paid from their own pay page
A subscriber who can’t buy a plan by role, tag or Buy once doesn’t see it among their Change plan options. Country locks don’t hide plans.
Limits
- A VPN changes the visitor’s country. If your server can be reached without the CDN, the header can be faked. Add a billing or shipping check where the lock matters.
- Administrators and shop managers are checked like any shopper. Let staff through with the filter below.
- Guests can’t pass a who-can-buy rule. They’re asked to log in.
- Buy once looks at past paid orders only. It doesn’t stop two of the product in the same order.
- FluentCRM and WP Fusion tags show only while those plugins are active. If you turn one off later, a rule that needs its tags refuses everyone.
- An empty billing or shipping country is refused by an address check.
- Frequently Bought Together doesn’t check the visitor’s country when it picks products, so its box can offer a locked product. The cart then refuses it.
- Express payment buttons on the product page stay visible for a country-locked product. The lock is checked when the order is placed.
- Grouped and external products have no Purchase rules tab. The products inside a grouped product use their own rules.
For Developers
stackless_purchase_rules_result receives the refusal code, the product and the user ID. The code is empty when the product can be bought, or one of login, denied, owned, country and address. Return an empty string to let the buyer through:
add_filter('stackless_purchase_rules_result', function ($code, $product, $userId) {
return $userId && user_can($userId, 'manage_woocommerce') ? '' : $code;
}, 10, 3);
More filters:
stackless_purchase_rules_for_product:array $rules, WC_Product $product, the resolved who and country rules.stackless_purchase_rules_country:string $country, string $source, to replace the detected visitor country.stackless_purchase_rules_geo_sources: the server keys read for the country, in order, with a label for each.stackless_purchase_rules_country_groups: country groups such asEU. Add your own as an uppercase key with a list of country codes.stackless_purchase_rules_exempt_cart_item:bool $exempt, array $cart_item, to skip the cart and address checks for one cart line.