We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    By using the below code, we can get customer group multi-select option with the help of system.xml.

    We can get all Customer group by system.xml in System -> Configuration Section of admin.

    Create system.xml file,
    Path, app/code/Magemonkeys/Customer/etc/adminhtml/system.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
        <system>
            <section id="catalog">
                <group id="customer_group" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <field id="list" translate="label" type="multiselect" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
                        <label>Customer Groups</label>
                        <source_model>MagemonkeysCustomerModelAdminhtmlSystemConfigSourceCustomerGroup</source_model>
                        <can_be_empty>1</can_be_empty>
                    </field>
                </group>
            </section>
        </system>
    </config>

    You need to get  all customer group using a Model file

    Create Group.php file,
    Path, app/code/Magemonkeys/Customer/Model/Adminhtml/System/Config/Source/Customer/Group.php

    <?php
    namespace MagemonkeysCustomerModelAdminhtmlSystemConfigSourceCustomer;
    
    class Group implements MagentoFrameworkOptionArrayInterface
    {
        public function __construct(MagentoCustomerModelResourceModelGroupCollectionFactory $groupCollectionFactory)
        {
            $this->_groupCollectionFactory = $groupCollectionFactory;
        }
    
        /**
         * @return array
         */
        public function toOptionArray()
        {
            if (!$this->_options) {
                $this->_options = $this->_groupCollectionFactory->create()->loadData()->toOptionArray();
            }
            return $this->_options;
        }
    }

    Clear Cache using php bin/magento cache:flush

    field_5bfb909c5ccae

      Get a Free Quote