- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016
07:57 AM
09-16-2016
07:57 AM
Hide products items for guests / non registered users
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.
Labels:
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2018
01:56 AM
01-25-2018
01:56 AM
Re: Hide products items for guests / non registered users
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; } }
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-29-2018
03:35 AM
05-29-2018
03:35 AM
Re: Hide products items for guests / non registered users
@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