2

I am trying to get the groups of a specific user to use that information for sorting products depending on who is logged in. How do I get the group name of a logged in user? I cannot find any documentation about this.

I am using Magento 2.

Thank you in advance!

1 Answer 1

1

You can get the group ID from a customer session.

\Magento\Customer\Model\Session

You can use the method getCustomerGroupId() from this class.

You can use that to determine your group.

I would recommend using group IDs for this, but you can also get additional group information from:

\Magento\Customer\Api\GroupRepositoryInterface

I hope this helps!

2
  • Thank you. I already know how to get the group ID of a user, but I am not sure about what to do with that information. Would you fetch the group name with a MySQL query or is there another way? My final goal is to be able to match the group name of a user to a specific attribute of a product to be able to sort out products. Commented Jan 31, 2017 at 7:41
  • 1
    Pass the group ID into the Service Contract GroupRepositoryInterface. Using the getById() method will return an instance of \Magento\Customer\Model\Data\Group. You can get group information from that model. Commented Feb 1, 2017 at 16:13

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.