What event do I use best to hide a product from customer?
I would like to show some products only to a customer group. Normal users should not be able to see them.
I would start with subscribing the
ProductListingCriteriaEvent
this at least should allow you do filter out the products you want to hide for product listings.
I believe this still would allow to show the detail pages, so you also should hook into
ProductPageLoadedEvent
and make your customer group checks.
Last but not least, you will probably run into caching problems, i.e. the page would be cached for the normal users, and the specific customer group would get the cached results, which still do not include "their" products.
We ran into that here: Shopware 6: Changing URL parameters returns the same result in prod mode
The solution for this would probably to subscribe to the ProductListingRouteCacheKeyEvent
$event->addPart((string)($currentCustomerGroup ? 'null'));
Also you might need to take care of hiding from the search.
This problem seems to be already solved with a paid plugin (https://store.shopware.com/en/acris84338660411/block-products-for-customer-groups.html) - but in case you implement it on your own, it would be cool if you publish your solution on GitHub :-)