The product or variant to check
Optionallocation: stringThe user's ISO 3166-1 alpha-2 location code (may be undefined)
True when the user may buy the option, false when it should be hidden
canUserBuy({ purchaseRestriction: { excludedCountries: ["US"] } }, "US"); // false
canUserBuy({ purchaseRestriction: { euOnly: true } }, "PL"); // true
canUserBuy({ purchaseRestriction: { declarationOfUseRequired: true } }, "US"); // true
canUserBuy({}, "US"); // true
export function canUserBuy(option: Variant, location?: string): boolean {
const restriction = option.purchaseRestriction;
if (!restriction) {
return true;
}
if (restriction.excludedCountries?.some((code) => code === location)) {
return false;
}
if (restriction.euOnly && (location === undefined || !EU_COUNTRY_CODES.has(location))) {
return false;
}
if (restriction.restrictedDelivery) {
return false;
}
if (restriction.buyerRestricted) {
return false;
}
return true;
}
Decides whether the current user may buy a single option (a product or one of its variants) given their location. Region, EU-only, unresolved-delivery, and buyer restrictions all exclude; a declaration-of-use requirement is informational and never excludes. An option with no restriction is buyable.
euOnlyis checked against EU_COUNTRY_CODES (the 27 EU member states), and an unknown user location is treated as ineligible for the safe EU-only case.