How to Add New Column on Admin Customer Group Page in Magento 2.X?

Feb 5, 2019341Andrey Masyuk
How to Add New Column on Admin Customer Group Page in Magento 2.X?

To add a new column on admin customer group page we will use extension attributes, which are new to Magento 2. They extend default functionality and often use more complex data types than custom attributes. These attributes do not appear on the GUI. This method is simple to implement, however, it is suitable for version 2.2 and earlier ones. First I will explain to you the implementation for 2.2 and earlier versions, and then we’ll proceed to 2.3.

To begin with, let’s create a custom Magento module for this task. Vendor will be Belvg, ModuleNameCustomer_group_ext. You can add a custom field to customer_group manually (with SQL query) or using InstallSchema (https://devdocs.magento.com/videos/fundamentals/add-a-new-table-to-database/).

I’ve added admin_comment field by query:

The module has a typical structure (with registration.php, module.xml), that is why I’m going to stop on important files we need.

app/code/Belvg/Customer_group_ext/etc/extension_attributes.xml:

app/code/Belvg/Customer_group_ext/view/adminhtml/layout/customer_group_index.xml:

You can see the result in the following screenshots:

customer group table web interface

Pic1 – customer_group table in web interface

grid view on customer group page

Pic2 – grid view on a customer group page

As you can see, we’ve extended customer_group table. Another way is to store custom fields in a separate table, for example, customer_group_ext_fields. In that case our SQL query:

Your extension_attributes.xml will look the following way:

customer_group_index.xml will be the same except 1 string:

NB! In Magento documentation, you can find ref parameter, which is optional and restricts access to the extension attribute to users with the specified permission. If you’re in developer mode, this parameter isn’t optional. If you don’t set it, the platform gives out the following error message:

error message magento 2

So, if you are working with the latest version of Magento at this moment, which is 2.3, you can just extend UI component in your custom module and use a new field in the database as I mentioned above. First, you need to create the following file:

app/code/Belvg/Customer_group_ext/view/adminhtml/ui_component/customer_group_listing.xml:

We’ll get the following result:

customer groups magento

Values for the new column are taken from the database (we are using the same customer_group table as in the example for 2.2):

values from the column magento 2

As you can see, it is easier and quicker to add a new column on admin customer group page in Magento 2.3. Here you don’t need to use extension attributes – only extend target ui_component and create a new field in the database.



2 Comments

  1. Nice article. I’m a new Magento dev and have been looking for recent, well made tutorials for a while. I only managed to find this site with a filtered by time search on google.

    Keep up the good work :)

  2. Hi, Noel!
    Thanks for your kind words, our authors really appreciate your feedback. Stay tuned for new Magento tutorials at our blog.

Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *