Magento 2 : get customer groups custome module

Magento 2 : get customer groups custome module

October 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 : get customer groups custome module

Displaying Customer Groups in Magento 2 Custom Module

When developing custom modules for Magento 2, you might want to display customer group information in your admin grid views. In this guide, we’ll explore how to show customer groups like “NOT LOGGED IN - General” or “NOT LOGGED IN - General - Wholesale - Retailer” in your module’s customer grid.

Understanding the Current Setup

You’re already on the right track with the provided code snippets. You’ve defined a new column for ‘customer_group’ using an options type renderer, which points to Rendy\ModuleWarehouse\Block\Adminhtml\Warehouse\Renderer\CustomerGroups. The CustomerGroups class extends the abstract renderer and attempts to fetch customer groups.

Enhancing Customer Group Rendering

The current implementation of the render() method in your CustomerGroups class doesn’t actually return any data. This is why you’re not seeing any customer group information on the grid.

Here’s how we can improve this:

namespace Rendy\ModuleWarehouse\Block\Adminhtml\Warehouse\Renderer;

class CustomerGroups extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
    protected $_customerGroup;

    public function __construct(
        \Magento\Backend\Block\Context $context,
        \Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupFactory, // Updated line
        array $data = []
    ) {
        $this->groupFactory = $groupFactory; // Renamed for clarity
        parent::__construct($context, $data);
    }

    public function render(DataObject $row)
    {
        $customerGroups = $this->groupFactory->create()->toOptionArray(); // Fetch groups

        if ($row->getCustomerGroup()) { // Check if customer group is set
            foreach ($customerGroups as $group) {
                if ($group['value'] === (string)$row->getCustomerGroup()) {
                    return $group['label']; // Return the corresponding group label
                }
            }
        }

        return ''; // If no match found, return an empty string
    }
}

Fetching and Displaying Customer Groups

In this updated version:

  1. We’re using a CollectionFactory instead of the raw model to fetch customer groups. This allows us to handle multiple websites if needed.

  2. In the render() method, we first check if the customer group is set for the current row. If it is, we iterate through our fetched customer groups and compare the stored value with the row’s customer group.

  3. Once a match is found, we return the corresponding label of that group. This will display the customer group name in your grid column.

  4. If no match is found (which can happen if the customer isn’t assigned to any group), we simply return an empty string. In most cases, this will display as “N/A” or nothing at all on the grid.

Conclusion

With these modifications, you should now be able to accurately display customer groups in your custom Magento 2 module’s admin grid views. This approach ensures that the data is fetched efficiently and displayed correctly based on the customer’s assigned group(s).

Remember, effective rendering requires a clear understanding of your data structure and how it maps to your grid column definitions.

For more advanced techniques and best practices for developing Magento 2 extensions, I recommend checking out our comprehensive guides at LaravelCompany.

Magento 2 Extensions Admin Customers

Our network