In the products page, I want to limit product variations based on user roles. For example, I only want admin to be able to see some of the product variation. I tried to achieve this by adding an extra field in variation and then checking that field in twig template (commerce-product.html.twig). Unfortunately, this method only works with rendered entity and I couldn't use the "add to cart" functionality. Is there a way to run a condition before displaying a product variation (along with the add to cart form)?

1 Answer 1


I solved this issue by subscribing to ProductEvents::FILTER_VARIATIONS event. You can write custom condition in your event subscriber. Reference: https://docs.drupalcommerce.org/commerce2/developer-guide/products/displaying-products/add-to-cart-form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.