How to create customer groups programmatically in Magento 2
To create customer groups programmatically in Magento 2, use $group->save() on the model returned by the GroupFactory:
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 33 34 35 36 |
namespace MyCompany\MyModule\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; /** * I'd like a customer group factory please Sir! */ public function __construct(GroupFactory $groupFactory) { $this->groupFactory = $groupFactory; } public function install( ModuleDataSetupInterface $setup, ModuleContextInterface $context ) { $setup->startSetup(); // Create the new group /** @var \Magento\Customer\Model\Group $group */ $group = $this->groupFactory->create(); $group ->setCode('My New Group'); ->setTaxClassId(3) // magic numbers OK, core installers do it?! ->save(); $setup->endSetup(); } } |
Source: