0

I need some functionality based on the response given by ERP.

I need to get the customer group id by using the customer group code?

How do I get customer group id by group name ?

1 Answer 1

0

You can filter the group name using the collection Filter. The below is a code that can be used with the block logic, but it can be used any other parts of the application.

Basically, we need to query this database table customer_group


protected $groupFactory;

/**
 * @param \Magento\Framework\View\Element\Template\Context $context
 * @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
 * @param array $data
 */
public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Customer\Model\GroupFactory $groupFactory,
    array $data = []
) {
    parent::__construct($context, $data);
    $this->groupFactory = $groupFactory;
}

/**
 * @param $id
 * @return string
 */
public function getGroupIdByName($name)
{
    $group = $this->groupFactory->create();

    $groupCollection = $group->getCollection();
    $groupCollection->addAttributetoFilter('customer_group_code',$name);

    return $groupCollection->getFirst();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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