6

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:

enter image description here

11

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
    ]
);
  • I would like to sat this great answer – Sourav Dec 9 '18 at 7:58
7

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

  • Class Magento\Customer\Model\Customer\Source\Group does not exist – Prashant Valanda Jun 27 '16 at 10:15
  • @PrashantValanda you probably made a typo: github.com/magento/magento2/blob/develop/app/code/Magento/… – Raphael at Digital Pianism Jun 27 '16 at 10:18
  • I am using magento 2.0.0. check here is no source folder prntscr.com/blm5ej – Prashant Valanda Jun 27 '16 at 10:21
  • I used this in a custom extension in the system config field as select option via <field id="public" translate="label" type="select" sortOrder="50" showInDefault="1" showInWebsite="0" showInStore="0"> <label>Public/Guest Group</label> <source_model>Magento\Customer\Model\Customer\Source\Group</source_model> </field> – BENN1TH Sep 29 '18 at 5:02

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.