Hi,
Is there a way to hide products to guests/non registered users in Magento 2?
I want to hide not only the price but also the products published. Is thats isn't possible, i would like to hide the price (i see some paid extensions, but is there another way?)
Thanks in advance.
First create one product level attribute to show/hide products for Guest User then create a front-end observer "catalog_product_collection_load_after"
<event name="catalog_product_collection_load_after"> <observer name="company_module_product_collection_load_after" instance="Company\Module\Observer\LoadProductsObserver"/> </event>
In Observer update collection :
public function execute(\Magento\Framework\Event\Observer $observer) { $collection = $observer->getEvent()->getCollection(); foreach($collection as $data){ if($this->canShow($data->getEntityId())){ // remove product from the collection $collection->removeItemByKey($data->getEntityId()); } } return $collection; } public function canShow($productId){ $product = $this->_productRepository->getById($productId); if($product->getCanShow()){ // Hide this product return TRUE; } else{ return FALSE; } }
@Devendra1991 wrote:First create one product level attribute to show/hide products for Guest User then create a front-end observer "catalog_product_collection_load_after"
<event name="catalog_product_collection_load_after"> <observer name="company_module_product_collection_load_after" instance="Company\Module\Observer\LoadProductsObserver"/> </event>In Observer update collection :
public function execute(\Magento\Framework\Event\Observer $observer) { $collection = $observer->getEvent()->getCollection(); foreach($collection as $data){ if($this->canShow($data->getEntityId())){ // remove product from the collection $collection->removeItemByKey($data->getEntityId()); } } return $collection; } public function canShow($productId){ $product = $this->_productRepository->getById($productId); if($product->getCanShow()){ // Hide this product return TRUE; } else{ return FALSE; } }
Toolbar isnt updating if we do it.
any solution it should update filter on right and toolbar too