0

I created a test category called Test added a test product and set category permission to Logged in user only.

enter image description here

so the category and product shouldn't be visible to non-logged-in customers. but when I use Search product it shows in Search Results.

enter image description here

and this is my category permission configuration.

enter image description here

Any Idea, Thanks.

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
{
    /**
     * @var ProductRepository
     */
    private ProductRepository $productRepository;
    /**
     * @var CustomerSessionFactory
     */
    private CustomerSessionFactory $customerSessionFactory;
    /**
     * @var PermissionCollectionFactory
     */
    private PermissionCollectionFactory $permissionCollectionFactory;

    /**
     * @param ProductRepository $productRepository
     * @param CustomerSessionFactory $customerSessionFactory
     * @param PermissionCollectionFactory $permissionCollectionFactory
     */
    public function __construct(
        ProductRepository $productRepository,
        CustomerSessionFactory $customerSessionFactory,
        PermissionCollectionFactory $permissionCollectionFactory
    ) {
        $this->productRepository = $productRepository;
        $this->customerSessionFactory = $customerSessionFactory;
        $this->permissionCollectionFactory = $permissionCollectionFactory;
    }

    /**
     * @throws NoSuchEntityException
     */
    public function execute(Observer $observer)
    {
        $isAllowed = false;
        $customerSession = $this->customerSessionFactory->create();
        if (!$customerSession->isLoggedIn()) {
            $items = $observer->getEvent()->getCollection()->getItems();
            foreach ($items as $item) {
                $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.