0

I have set category permission to deny for non-logged-in users and in the database values are saved accordingly. enter image description here

but when I search product on front end it shows the wrong permission.

\Magento\CatalogPermissions\Observer\ApplyProductPermissionOnCollectionAfterLoadObserver

enter image description here

any idea?

1 Answer 1

0

I could not figure out why it was behaving this way, but created an alternate solution to fix this issue.

Pyaray/CatalogPermission/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_collection_load_after">
        <observer name="magento_catalogpermissions_"
                  instance="Pyaray\CatalogPermission\Observer\ApplyProductPermissionOnCollectionAfterLoadObserver"/>
    </event>
</config>

Pyaray\CatalogPermission\Observer\ApplyProductPermissionOnCollectionAfterLoadObserver

<?php
    declare(strict_types=1);
    
    namespace Pyaray\CatalogPermission\Observer;
    
    use Magento\CatalogPermissions\Model\PermissionFactory;
    use Magento\Framework\Event\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Catalog\Model\ProductRepository;
    use Magento\Framework\Exception\NoSuchEntityException;
    use Magento\Customer\Model\SessionFactory as CustomerSessionFactory;
    use Magento\CatalogPermissions\Model\ResourceModel\Permission\CollectionFactory as PermissionCollectionFactory;
    
    class ApplyProductPermissionOnCollectionAfterLoadObserver implements  ObserverInterface
    {
        
        private ProductRepository $productRepository;
      
        private CustomerSessionFactory $customerSessionFactory;
       
        private PermissionCollectionFactory $permissionCollectionFactory;
    
        public function __construct(
            ProductRepository $productRepository,
            CustomerSessionFactory $customerSessionFactory,
            PermissionCollectionFactory $permissionCollectionFactory
        ) {
            $this->productRepository = $productRepository;
            $this->customerSessionFactory = $customerSessionFactory;
            $this->permissionCollectionFactory = $permissionCollectionFactory;
        }
    
       
        public function execute(Observer $observer)
        {
            $customerSession = $this->customerSessionFactory->create();
            if (!$customerSession->isLoggedIn()) {
                $items = $observer->getEvent()->getCollection()->getItems();
                foreach ($items as $item) {
$isAllowed = false;
                    $product = $this->productRepository->getById($item->getEntityId());
                    $categoryIds = $product->getCategoryIds();
                    foreach ($categoryIds as $categoryId) {
                        $permission = $this->permissionCollectionFactory->create();
                        $permissions = $permission->addFieldToFilter('category_id', ['eq'=>$categoryId])
                        ->addFieldToFilter('customer_group_id',['eq'=>0])->getData();
                        if ($permissions) {
                            foreach ($permissions as $permission) {
                                if ($permission['grant_catalog_category_view'] == -1) {
                                    $isAllowed = true;
                                }
                            }
                        }
                    }
                    if($isAllowed == false)
                    {
                        $observer->getEvent()->getCollection()->removeItemByKey($item->getId());
                    }
                }
            }
        }
    }

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.