How to Create a Customer Group Programmatically

Today we will have a look together on how you can create a customer group programmatically. Like this we a happy that we manage to reach 90 blog posts about Magento 2!

We have only to create a data patch class as the one below. The apply() function is the one that includes the code which is executed by running the CLI command, bin/magento setup:upgrade.

There a two things that we need to define while creating a customer group. The customer group name and the tax class of the customer group.

As you can see above, our class implements two interfaces, the DataPatchInterface and the PatchRevertableInterface. The DataPatchInterface is the standard one for a data patch class, requests to have in our class the functions apply(), getDependencies() and getAliases(). The PatchRevertableInterface requests to have in our class the revert() function which is called by module uninstalling.

So now if you execute the command bin/magento setup:upgrade, our data patch will be executed and store configuration will be set.

The full example as extension can be found here.

Feel free to share this post and ask your questions in the comments below.

Till next time!

DigitalOcean Referral Badge