How To

How to Create Customer Groups Programmatically in Magento 2

Hello Magento Friends,

Today I am going to provide a solution on How to Create Customer Groups Programmatically in Magento 2.

Let’s see the requirement for customer groups and steps to create customer groups in Magento 2.

Why is it necessary to Create Customer Groups in Magento 2?

Customers are the most important factor of consideration for E-commerce shop owners. A customer might not be interested in everything you sell or all the products of your Magento store may not be suitable for all types of customers. This is where the Magento store owners need to classify customers based on age, location, interests, and more. This can be done by creating Customer Groups. Customer groups can help to provide a personalized shopping experience to customers and show the right product to the right audience.

Default Magento provides the feature to group customers from the backend configuration. Instead integrate Auto Customer Group Switching for Magento 2 to automatically move customers to a specific group based on conditions.

Say, now you have a requirement to create a custom customer group for your Magento 2 store. Move forward with this article, to accomplish How to Create Customer Groups Programmatically in Magento 2.  

Steps to Create Customer Groups Programmatically in Magento 2:

Step 1: Create InstallData.php at the given below path

app\code\Vendor\Extension\Setup\InstallData.php

Now, add the below code

<?php
namespace Vendor\Extension\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\GroupFactory;

class InstallData implements InstallDataInterface
{
    protected $groupFactory;
    
    public function __construct(GroupFactory $groupFactory) 
    {
        $this->groupFactory = $groupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) 
    {
        $setup->startSetup();
        $group = $this->groupFactory->create();
        
        /* For Creating a Single Customer Group */        $group->setCode('New Customer Group')
            ->setTaxClassId(3)
            ->save();
        
        /* For Creating a Multiple customer group */ 
        $setup->getConnection()->insertForce(
            $setup->getTable('customer_group'),
            ['customer_group_code' => 'NewCustomerGroup1', 'tax_class_id' => 3]
        );
        $setup->getConnection()->insertForce(
            $setup->getTable('customer_group'),
            ['customer_group_code' => 'NewCustomerGroup2', 'tax_class_id' => 3]
        );
        
        $setup->endSetup();
    }
}

Conclusion:

This way you can Create Customer Groups Programmatically in Magento 2. Once you have created a customer group, you can easily Manage Customer and Customer Groups in Magento 2 using the admin panel.

If you face any hardship with the above steps, feel free to get in touch with me via the comment section. Share the article with your developer friends. Continue to check our latest updates.

Happy Coding!

Click to rate this post!
[Total: 6 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago