How to show only products that are allowed to logged in user in Magento?

How to show only products that are allowed to logged in user in Magento?

September 16, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to show only products that are allowed to logged in user in Magento?

Optimizing Product Visibility Based on User Access in Magento

In today’s interconnected e-commerce landscape, controlling product visibility based on customer access is crucial for maintaining data privacy and security. In this post, we’ll explore how to manage product access rights for different types of users within the Magento ecosystem.

Understanding the Challenge

You’ve described a scenario where products need to be categorized into three groups:

  1. Products accessible to all logged-in or non-logged-in users.
  2. Products exclusively accessible to Customer A (Set A).
  3. Products exclusively accessible to Customer B (Set B).

The goal is to dynamically adjust product visibility based on the currently logged-in customer’s identity.

Magento Approach: Extending Core Functionality

To achieve this, we’ll need to extend the core Magento functionality by creating a custom module that hooks into the product collection retrieval process. This approach allows us to maintain the flexibility and performance of the native platform while introducing our specialized user access control logic.

Here’s an outline of the steps involved:

  1. Create a custom module: Follow standard Magento best practices for creating a new module within your app/code directory. This will provide a clean, structured environment for your code.

  2. Override the product collection: Create a plugin or an observer that intercepts the product collection generation process before it’s displayed on the frontend. You can do this by using Magento’s Plugin System or Observer pattern.

  3. Implement user access control: Within your custom logic, retrieve the currently logged-in customer and compare their ID with the allowed customer IDs for each product. Filter out products that don’t match the current user’s access level before the collection is returned to the frontend.

Here’s a simple example of what this might look like using Magento 2.x:

public function aroundGetLoadedProductCollection(\Magento\Catalog\Model\ResourceModel\Product\Collection $collection, callable $proceed)
{
    if ($collection->getFlag('is_persistent')) {
        return $proceed($collection);
    }

    $allowedCustomerIds = [
        'customer_a_id' => [101, 102, 103], // Replace with actual IDs
        'customer_b_id' => [501, 502] // Replace with actual IDs
    ];

    $currentUserId = ...; // Retrieve the current user ID

    if (isset($allowedCustomerIds[$currentUserId])) {
        $collection->addFieldToFilter('entity_id', ['in' => $allowedCustomerIds[$currentUserId]]);
    } else {
        $collection->addFieldToFilter('entity_id', ['nin' => array_merge(...$allowedCustomerIds)]);
    }

    return $collection;
}

Performance Considerations

It’s essential to optimize this process for production environments. Avoid using expensive database queries or heavy calculations within the loop that processes each product.

Instead, consider maintaining a separate database table (e.g., customer_product_access) that maps customer IDs to allowed product IDs. Updating this mapping only when necessary will minimize the performance impact on your store’s frontend.

Conclusion

Managing dynamic product visibility based on user access in Magento requires a combination of custom module development and strategic use of the platform’s built-in features.

By extending the core collection retrieval process, you can maintain the native Magento experience while implementing specialized business logic to control product access rights. This approach ensures a clean separation between your custom functionality and the core platform code.

Remember to consult the Magento 2 Developer Documentation for detailed guides on creating plugins, observers, and modules within the Magento ecosystem.

With this strategy in place, you can effectively manage product access rights while providing a seamless user experience tailored to each customer’s permission level.

Catalog Performance Security Extensions

Our network