1) Model Factory way
We should try with \Magento\Customer\Model\Group
dev/tests/integration/testsuite/Magento/Weee/Model/TaxTest.php
$fixtureGroupCode = 'custom_group';
$fixtureTaxClassId = 3;
/** @var \Magento\Customer\Model\Group $group */
$group = Bootstrap::getObjectManager()->create(\Magento\Customer\Model\Group::class);
$fixtureGroupId = $group->load($fixtureGroupCode, 'customer_group_code')->getId();
Avoiding using Object Manager. So, should inject \Magento\Customer\Model\GroupFactory
in the constructor.
2) Object Repository:
<?php
namespace Vendor\Customer\Model;
use Magento\Customer\Api\GroupRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;
class Test
{
private $customerGroups;
protected $groupRepository;
protected $searchCriteriaBuilder;
protected $filterBuilder;
public function __construct(
GroupRepositoryInterface $groupRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder
) {
$this->groupRepository = $groupRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
}
public function testGetGroup()
{
$generalFilter[] = $this->filterBuilder
->setField('customer_group_code')
->setConditionType('eq')
->setValue('General')
->create();
$searchCriteria = $this->searchCriteriaBuilder
->addFilters($generalFilter)
->create();
if ($this->customerGroups === null) {
$this->customerGroups = [];
foreach ($this->groupRepository->getList($searchCriteria)->getItems() as $item) {
$this->customerGroups[$item->getId()] = $item->getCode();
}
}
var_dump($this->customerGroups);
}
}