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
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