Display custom block on category or product page only if user is logged in and is joining some specific groups

Display custom block on category or product page only if user is logged in and is joining some specific groups

October 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Display custom block on category or product page only if user is logged in and is joining some specific groups

Displaying Custom Content Based on User Groups: A Magento Developer’s Perspective

In Magento, displaying dynamic content based on user groups isn’t as straightforward as it might seem in other platforms like WordPress or Drupal. However, with the right approach and some custom coding, we can achieve this functionality quite effectively.

Understanding Magento Static Blocks

Static blocks are reusable chunks of HTML content that you can embed anywhere in your store using a simple unique identifier called block ID. They’re ideal for inserting common content across multiple pages without having to duplicate code.

To create a new static block:

  1. Navigate to Content → Elements → Block.
  2. Click on “Add New Block”.
  3. Enter a unique Block Title and Identifier.
  4. Add your HTML content in the Content field.
  5. Save the block.

Displaying Static Blocks Conditionally

The challenge lies in showing this static block only for logged-in users who belong to specific groups, such as ‘reseller’, ‘general’, or ‘clients’. We’ll need to create a custom module and override the product view template file.

Creating a Custom Module

First, let’s create the necessary directory structure for our new module:

/app/code/MyCompany/DisplayBlock
    /etc
        /di.xml
    /registration.php

In registration.php, we’ll include the following code to register our module:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MyCompany_DisplayBlock',
    __DIR__
);

Next, in di.xml, we’ll define the dependencies for our custom block:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\Template">
        <arguments>
            <argument name="template" xsi:type="string">MyCompany_DisplayBlock::block.phtml</argument>
        </arguments>
    </type>
</config>

Overriding the Product View Template

Now, let’s override the product view template file to include our custom block.

  1. Create a new directory structure: /app/design/frontend/{vendor}/{theme}/Magento_Catalog/templates/product/view.phtml
  2. Open view.phtml and find where you want to place your static block.
  3. Add the following code to call your static block:
<?php echo $block->getLayout()->createBlock('MyCompany\DisplayBlock\Block')->setTemplate('MyCompany_DisplayBlock::display.phtml')->toHtml(); ?>

Creating the Display Block

Finally, create a new file at /app/code/MyCompany/DisplayBlock/view/frontend/templates/display.phtml:

<?php if (isset($_SESSION['customer_id']) && in_array('reseller', $_SESSION['allowed_groups'])): ?>
    <div class="my-company-custom-block">
        <?php echo $this->getLayout()->createBlock('Magento\Cms\Block')->setBlockId('your_block_identifier')->toHtml(); ?>
    </div>
<?php endif; ?>

Replace 'reseller' with the specific group you want to target, and update 'your_block_identifier' with the actual ID of your static block.

Conclusion

Displaying custom content based on user groups in Magento requires a combination of creating static blocks and overriding template files using a custom module. By following the steps outlined above, you can effectively display targeted content in your product pages or sidebars for logged-in users who belong to specific groups.

Remember to clear your cache after making these changes. For more detailed tutorials on Magento development, feel free to explore the LaravelCompany Magento Development Blog.

This approach ensures that you maintain a clean and organized codebase while providing a tailored user experience for your store’s customers.

Catalog Extensions Themes CLI

Our network