Milind Daraniya

How To Create Customer Group Programmatically In Magento 2

Published September 13th, 2023 11 min read

In Magento 2, you might need to create customer groups programmatically as part of custom module development or specific business requirements. Programmatically creating customer groups allows you to automate the process and ensure consistency across your Magento store. In this tutorial, we'll guide you on how to create a customer group programmatically in Magento 2.

Step 1: Set Up the Module

First, create a custom module if you haven't already. You can create a custom module using Magento's module creation command:

php bin/magento module:create --code Vendor_ModuleName

Step 2: Create the Customer Group

In your custom module, create a PHP file where you'll define the logic to create the customer group. Let's assume the file is Vendor/ModuleName/Setup/InstallData.php.

namespace Vendor\ModuleName\Setup;

use Magento\Customer\Api\Data\GroupInterfaceFactory;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    /**
     * @var GroupInterfaceFactory
     */
    private $groupFactory;

    /**
     * @var GroupRepositoryInterface
     */
    private $groupRepository;

    public function __construct(
        GroupInterfaceFactory $groupFactory,
        GroupRepositoryInterface $groupRepository
    ) {
        $this->groupFactory = $groupFactory;
        $this->groupRepository = $groupRepository;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->createCustomGroup();
    }

    private function createCustomGroup()
    {
        $groupName = 'Custom Group'; // Replace with your desired group name

        $group = $this->groupFactory->create();
        $group->setCode('custom_group_code'); // Replace with your desired group code
        $group->setTaxClassId(3); // Replace with the tax class ID you want to assign to the group (optional)

        try {
            $this->groupRepository->save($group);
            echo 'Customer Group "' . $groupName . '" created successfully.';
        } catch (\Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }
}

Step 3: Run the Setup Upgrade

Now, you need to run the setup upgrade to apply the changes and create the customer group:

php bin/magento setup:upgrade

Step 4: Verify the Customer Group

You can verify that the customer group was created by checking the Magento admin panel. Navigate to "Customers" > "Customer Groups" and see if the custom group is listed.

By following this tutorial, you have successfully created a customer group programmatically in Magento 2 using a custom module. Programmatically creating customer groups allows you to automate the process and ensures consistency across your Magento store. This can be especially useful when you need to create custom customer groups based on specific criteria or business rules. Happy coding! 🛍️👥