Hello Everyone,
In this blog, we will learn about how to Get Customer Groups in Magento 2.
Magento 2 has following customer groups:
- General
- Not Logged In
- Retailer
- Wholesale
Sometimes we need to get a customer group so use the below solution for that.
Without wasting your time, let us guide you straight away. Follow the easy step given below to get Customer Group in Magento 2.
STEPS FOR GET CUSTOMER GROUP IN MAGENTO 2
Step 1: Create Data.php file
app/code/Vendor/Extension/Helper/Data.php
<?php
namespace Vendor\Extension\Helper;
use Magento\Customer\Model\ResourceModel\Group\Collection as CustomerGroup;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
class Data extends AbstractHelper
{
protected $customerGroup;
public function __construct(
Context $context,
CustomerGroup $customerGroup
)
{
$this->customerGroup = $customerGroup;
parent::__construct($context);
}
public function getCustomerGroups()
{
$customerGroups = $this->customerGroup->toOptionArray();
return $customerGroups;
}
}
In above code get Customer Groups() will return the array of all customer groups. So, you can use this function as per your requirements.
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can get a customer group in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
