Is there an out-of-the-box way to restrict purchases by certain user roles?
I wanted to check if FluentCart already supports (or plans to support) role-based purchase restrictions β for example, preventing certain roles like administrator from buying specific products or all products.
I couldnβt find a built-in setting or filter for this, so I implemented it using FluentCart hooks and the Cart API.
This is the approach Iβm currently using β just want to confirm:
- Whether thereβs an official/native way to do this
- If not, whether this approach is considered correct/safe
- And if role-based purchase restriction is something that might be added natively in the future
define('BLOCKED_PURCHASE_ROLES', ['administrator']);
define('MEMBERSHIP_PRODUCT_IDS', [1, 2, 3]);
add_action('fluent_cart/cart/item_added', 'block_roles_from_purchasing', 10, 1);
add_action('fluent_cart/cart/cart_completed', 'block_roles_from_purchasing', 10, 1);
function block_roles_from_purchasing($data) {
if (!is_user_logged_in() || empty($data['cart'])) {
return;
}
$user = wp_get_current_user();
if (!array_intersect((array) $user->roles, BLOCKED_PURCHASE_ROLES)) {
return;
}
$cart = $data['cart'];
foreach ($cart->getItems() as $item) {
$product_id = (int) $item['product_id'];
if (in_array($product_id, MEMBERSHIP_PRODUCT_IDS, true)) {
$cart->removeItem($product_id, [], true);
}
}
}
This is effectively the same method I am using except I am doing it by FluentCRM tags instead of roles.
This approach may work only with the standard cart flow. But it won't work with Direct Checkout.
Direct checkout bypasses the cart lifecycle, so hooks like fluent_cart/cart/item_added and cart_completed donβt fire.
At the moment, FluentCart docs donβt expose any documented hooks or native options to enforce role-based purchase restrictions during direct checkout.
Do you have any solution Shahjahan JewelΒ ?
Rupesh PadhiΒ I'm thinking today's new release with ghost products may be useful for this
Kyle Dee Maybe. Ghost products are new to me. I havenβt tested them yet with Direct Checkout. If they change the checkout flow or expose hooks, it could work. Any docs or example?