{"id":725,"date":"2019-12-31T07:27:52","date_gmt":"2019-12-31T07:27:52","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/12\/31\/create-customer-group-programmatically-in-magento-2\/"},"modified":"2025-07-23T10:36:04","modified_gmt":"2025-07-23T05:06:04","slug":"create-customer-group-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-customer-group-programmatically-in-magento-2\/","title":{"rendered":"How To Create Customer Group Programmatically In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">After <a href=\"https:\/\/meetanshi.com\/blog\/create-customers-programmatically-in-magento-2\/\" target=\"_blank\" rel=\"noopener noreferrer\" data-cke-saved-href=\"https:\/\/meetanshi.com\/blog\/create-customers-programmatically-in-magento-2\/\">creating customers programmatically in Magento 2<\/a>, you may require to group them based on similar buying behavior, let\u2019s say, to organize them with ease, give them discounts, and restrict access to specific products or categories. Magento 2 offers a solution to divide the customers into groups called <a href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/customers\/customer-groups\" target=\"_blank\" rel=\"noopener noreferrer\" data-cke-saved-href=\"https:\/\/experienceleague.adobe.com\/en\/docs\/commerce-admin\/customers\/customer-groups\" data-cke-saved->Magento 2 Customer Groups<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As when you want to implement any functionalities based on the Magento 2 customer groups you need to <a title=\"How to Add Customer Group Field in Magento 2 Admin UI Component Form\" href=\"https:\/\/meetanshi.com\/blog\/add-customer-group-field-in-magento-2-admin-ui-component-form\/\">add customer group field in Magento 2 admin UI component form<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This arrangement helps store owners with targeted customer management and marketing. Efficiently manage the user experience offered, rules, tax classes, etc. based on the customer groups.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default Magento 2 customer groups are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>General<\/li>\n\n\n\n<li>Not Logged In<\/li>\n\n\n\n<li>Wholesale<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">However, the default features are never enough for us developers, (or can we say, for client requirements  )<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Managing user experience and store customization can be made easier with custom customer groups! And that\u2019s why I\u2019ve come up with the method to <strong>create customer group programmatically in Magento 2<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What can you do with the customers divided into groups in Magento 2 store?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Manage the content displayed to the users<\/li>\n\n\n\n<li>Configure tax rules<\/li>\n\n\n\n<li>Design discount and sales strategy<\/li>\n\n\n\n<li>Restrict product or categories<\/li>\n\n\n\n<li>Offer specific shipping or payment methods<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The list is endless. Offer anything you can think of that is related to customer types. The customers are going to be thankful for this win-win situation where you make their shopping experience smoother!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to create customer group programmatically in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create <strong><em>InstallData.php<\/em><\/strong>&nbsp;file at <strong>app\/code\/Vendor\/Extension\/Setup<\/strong> directory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Setup;\n\nuse Magento\\Framework\\Module\\Setup\\Migration;\nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\nuse Magento\\Customer\\Model\\GroupFactory;\n\nclass InstallData implements InstallDataInterface\n{\n    protected $groupFactory;\n\n    public function __construct(GroupFactory $groupFactory) {\n        $this->groupFactory = $groupFactory;\n    }\n\n    public function install(\n        ModuleDataSetupInterface $setup,\n        ModuleContextInterface $context\n    ) {\n        $setup->startSetup();\n\n        $group = $this->groupFactory->create();\n        $group\n            ->setCode('New Group')\n            ->setTaxClassId(3) \n            ->save();\n\n        $setup->endSetup();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also follow another method to <a href=\"https:\/\/meetanshi.com\/blog\/configure-customer-group-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">configure a customer group in Magento 2<\/a> from admin panel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with fellow developers and store owners via social media platforms.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After creating customers programmatically in Magento 2, you may require to group them based on similar buying behavior, let\u2019s say, to organize them with ease,&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-725","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=725"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/725\/revisions"}],"predecessor-version":[{"id":19141,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/725\/revisions\/19141"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}