Skip to content
NewRedesigned blogFeedback
Jigar Karangiya

Magento 2 UI Components and Visibility Conditions

Have you ever wanted to show or hide certain parts of your Magento store based on specific conditions? For example, showing special offers only to VIP customers or displaying different content in different store views?

Magento 2 UI Components and Visibility Conditions
Written by
JKJigar Karangiya
Published
Reading time
2 min read

Jigar Karangiya · · 2 min read

Introduction

Have you ever wanted to show or hide certain parts of your Magento store based on specific conditions? For example, showing special offers only to VIP customers or displaying different content in different store views? This is where Magento’s UI Components (opens in new tab) and Visibility Conditions come into play.

What are UI Components?

Think of UI Components as building blocks for your Magento store’s frontend. They combine:

  • JavaScript (for interactivity)
  • HTML (for structure)
  • CSS (for styling)
  • XML (for configuration)

These components make it easy to create consistent and reusable parts of your store’s interface.

What are Visibility Conditions?

Visibility Conditions are like smart switches that control when a UI Component should be shown or hidden. They help you create dynamic interfaces that change based on:

  • Who is viewing the page (customer group)
  • Where they are viewing from (store view)
  • What permissions they have
  • And many other conditions

Example: Customer Group Based Visibility

Let’s create a simple example that shows special offers only to specific customer groups.

Step 1: Create the PHP Class

First, we need to create a PHP class that decides when to show our component:

php
<?php
namespace Jigar\Blogs\Model\Condition;
 
use Magento\Framework\View\Layout\Condition\VisibilityConditionInterface;
use Magento\Customer\Model\Session;
 
class CustomerGroupVisibility implements VisibilityConditionInterface
{
    private static $conditionName = 'customer_group_visibility';
 
    private $customerSession;
 
    public function __construct(Session $customerSession)
    {
        $this->customerSession = $customerSession;
    }
 
    public function isVisible(array $arguments): bool
    {
        //without arguments you can also write your custom requirement here
        //and based on it you can return true/false
 
        // Get allowed customer groups from configuration
        $allowedGroups = $arguments['allowed_groups'] ?? [];
        
        // Get current customer's group
        $currentGroupId = $this->customerSession->getCustomerGroupId();
        
        // Check if current customer's group is allowed
        return in_array($currentGroupId, $allowedGroups);
    }
 
    public function getName(): string
    {
        return self::$conditionName;
    }
}

Step 2: Configure in XML

Next, we tell Magento when to use our visibility condition:

xml
<uiComponent name="special_offers">
    <visibilityCondition name="customer_group_visibility" 
        className="Vendor\Module\Model\Condition\CustomerGroupVisibility">
        <argument name="allowed_groups" xsi:type="array"> <!-- Optional if custom logic -->
            <item name="0" xsi:type="number">1</item> <!-- General customers -->
            <item name="1" xsi:type="number">2</item> <!-- Wholesale customers -->
        </argument>
    </visibilityCondition>
</uiComponent>

Step 3: Create the UI Component

Now, let’s create the actual component that will show our special offers:

xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">special_offers.special_offers_data_source</item>
        </item>
    </argument>
    <dataSource name="special_offers_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Vendor\Module\Model\ResourceModel\SpecialOffers\Grid\Collection</argument>
            <argument name="name" xsi:type="string">special_offers_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
        </argument>
    </dataSource>
    <columns name="special_offers_columns">
        <column name="title">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Title</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Common Uses

You can use visibility conditions for:

  • Showing special prices to VIP customers
  • Displaying different content in different store views
  • Showing admin-only features
  • Displaying region-specific promotions

Visibility Conditions are a powerful way to create dynamic, personalized experiences in your Magento store. They help you show the right content to the right people at the right time.

I hope this guide helps you understand how to use visibility conditions in Magento 2. If you have any questions, feel free to ask in the comments below!

You may also like,

Mastering Plugins in Magento 2: A Beginner-to-Expert Guide

How to Fetch Specific Column Values from Any Collection in Magento 2

How to Install the Hyvä Theme in Magento 2

Keep reading
All posts

01 · 3 min · Shares Adobe Commerce Core, Quick Notes

Deep Dive into Magento 2 Events and Observers

In this tutorial, I will guide you through the popular events and observers which works based on event driven architecture which is also referred as publish-subscribe pattern.

3 min

02 · 2 min · Shares Adobe Commerce Core, Quick Notes

How to Remove Sample Data from Magento 2

Discover the step-by-step process of removing sample data from your Magento 2 installation in my latest blog post. I’ll guide you through accessing your server, navigating to the Magento 2 directory, and using Magento’s command-line tool to remove sample data.

2 min

03 · 2 min · Shares Adobe Commerce Core, Quick Notes

Magento 2: How to use helper in PHTML?

Magento 2, an open-source e-commerce platform, offers a variety of tools for developers to create robust online stores. One such tool is the Helper class, a collection of methods that can be used globally.

2 min

Newsletter

New Magento tutorials, straight to your inbox

Code examples, performance tips, and fixes for common Magento issues. No spam.

Subscribe