How To Create Customer Group Programmatically In Magento 2
After creating customers programmatically in Magento 2, you may require to group them based on similar buying behavior, let’s 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 Magento 2 Customer Groups.
As when you want to implement any functionalities based on the Magento 2 customer groups you need to add customer group field in Magento 2 admin UI component form.
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.
The default Magento 2 customer groups are:
- General
- Not Logged In
- Wholesale
However, the default features are never enough for us developers, (or can we say, for client requirements 😉)
Managing user experience and store customization can be made easier with custom customer groups! And that’s why I’ve come up with the method to create customer group programmatically in Magento 2.
What can you do with the customers divided into groups in Magento 2 store?
- Manage the content displayed to the users
- Configure tax rules
- Design discount and sales strategy
- Restrict product or categories
- Offer specific shipping or payment methods
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!
Steps to create customer group programmatically in Magento 2:
Create InstallData.php file at app/code/Vendor/Extension/Setup directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php namespace Vendor\Extension\Setup; use Magento\Framework\Module\Setup\Migration; 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();     $group       ->setCode('New Group')       ->setTaxClassId(3)       ->save();     $setup->endSetup();   } } |
That’s it.
You can also follow another method to configure a customer group in Magento 2 from admin panel.
Any doubts with customer groups or how to create one can be mentioned in the Comments section below. I’d solve them asap.
Do share the solution with fellow developers and store owners via social media platforms.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How To Get Payment Method Title Of Order In Magento 2
Meetanshi Magento Extensions Launches and Updates December [2019]
Next