How to get all list of customer group?
-
30-09-2020 - |
Pregunta
I have created admin form. I am able to get customer group list using below code, but there is no "NOT LOGIN" group name so how can I get all group list?
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupOptions = $objectManager->get('\Magento\Customer\Model\Customer\Attribute\Source\Group')->getAllOptions();
$fieldset->addField(
'customer_group',
'multiselect',
[
'name' => 'customer_group[]',
'label' => __('Customer Group'),
'title' => __('Customer Group'),
'values' => $groupOptions,
'disabled' => $isElementDisabled
]
);
I am getting this output:
Solución
I was able to get solution using the below code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$groupOptions = $objectManager->get('\Magento\Customer\Model\ResourceModel\Group\Collection')->toOptionArray();
$fieldset->addField(
'customer_group',
'multiselect',
[
'name' => 'customer_group[]',
'label' => __('Customer Group'),
'title' => __('Customer Group'),
'values' => $groupOptions,
'disabled' => $isElementDisabled
]
);
Otros consejos
You can use the following class: Magento\Customer\Model\Customer\Source\Group
.
It provides a toOptionArray
method that you can use to get an array of customer groups
Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange