Magento 2: Create customer group programatically
May as well answer this since I figured it out.
The problem with my example above is that I was trying to use the GroupRepository to push the new model to the database.
Instead, I should have just used $group->save()
on the model returned by the GroupFactory:
<?php
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();
}
}
Since the version 2.2.1 the use of the ->save()
method from the Magento\Customer\Model\Group
implementations has been deprecated.
Instead, you should use:
use Magento\Customer\Api\Data\GroupInterfaceFactory;
use Magento\Customer\Api\GroupRepositoryInterface;
instead
use Magento\Customer\Model\GroupFactory;
Using Robbie Averill solution as a baseline: https://magento.stackexchange.com/a/93789/15530
Pass those classes to the constructor like:
public function __construct(
GroupInterfaceFactory $groupFactory,
GroupRepositoryInterface $groupInterface) {
$this->groupFactory = $groupFactory;
$this->groupInterface = $groupInterface;
}
And then just use it as per this example:
$this->groupInterface->save($group);
Where group is created using the factory creator like:
$group = $this->groupFactory->create();
Save method is deprecated in Magento 2 try this code:
<?php
/**
* Copyright © 2018 MyCompany. All rights reserved.
* See COPYING.txt for license details.
*/
namespace MyCompany\MyModule\Setup;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Customer\Api\Data\GroupInterfaceFactory;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* Class InstallData
*
* @package MyCompany\MyModule\Setup
*/
class InstallData implements InstallDataInterface
{
/**
* @var GroupInterfaceFactory
*/
private $groupFactory;
/**
* @var GroupRepositoryInterface
*/
private $groupRepository;
/**
* InstallData constructor.
*
* @param GroupInterfaceFactory $groupFactory
* @param GroupRepositoryInterface $groupRepository
*/
public function __construct(
GroupInterfaceFactory $groupFactory,
GroupRepositoryInterface $groupRepository
) {
$this->groupFactory = $groupFactory;
$this->groupRepository = $groupRepository;
}
/**
* Installs data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
try {
// Create the new group
/** @var \Magento\Customer\Model\Group $group */
$group = $this->groupFactory->create();
$group
->setCode('My New Group')
->setTaxClassId(3);
$this->groupRepository->save($group);
} catch (\Exception $e) {
}
$setup->endSetup();
}
}