3

How to load customer group by customer group name in Magento 2?

I am using below code in M1:

$companyClass = 'General';  
$groupObj = Mage::getModel('customer/group')->load($companyClass, 'customer_group_code');
echo '<pre>'; print_r($groupObj->getData()); echo '</pre>';

How to get this on Magento 2?

5

After review the magento2 code I able to get the below solution:-

protected $group;

public function __construct(
    ...
    \Magento\Customer\Model\Group $group,
    ...     
)
{
    ...
    $this->group = $group;
    ...
}

Use below code where you want to load the customer group:-

$cusGrpCode = 'General';
$groupObj = $this->group;
$existingGroup = $groupObj->load($cusGrpCode, 'customer_group_code');
echo '<pre>'; print_r($existingGroup->getData()); echo '</pre>';
| improve this answer | |
1

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);
    }
}
| improve this answer | |
0
namespace Namespace\Module\Helper;

use Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory;

/**
 * Class Product
*/
class Product
{


/**
 * @var CollectionFactory
 */
private $attributeSetCollection;


/**
 * Product constructor.
 * @param CollectionFactory $attributeSetCollection
 */
public function __construct(
    CollectionFactory $attributeSetCollection
) {
    $this->attributeSetCollection = $attributeSetCollection;
}



/**
 * @param string $attributeSetName
 * @return int
 */
public function getAttributeSetIdByName(string $attributeSetName): int
{
    $attributeSetCollection = $this->attributeSetCollection->create()
        ->addFieldToSelect('attribute_set_id')
        ->addFieldToFilter('attribute_set_name', $attributeSetName)
        ->getFirstItem();

    return (int) $attributeSetCollection->getAttributeSetId();
}
}

We can use the other fields or * in the addFieldToSelect.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.