2

I want to provide customer group based configuration in my custom group. So need to add customer group multiselect dropdown in system configuration at admin in Magento 2.x

2 Answers 2

5

Here is the code which I have used in my custom module. Hope this will also help you.

  1. Add below code in Vendor/Module/etc/adminhtml/system.xml file.
<?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>
    <tab id="custom_tab" translate="label" sortOrder="300">
        <label>Custom Tab</label>
    </tab>
    <section id="custom_section" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>Custom Module</label>
        <tab>custom_tab</tab>
        <resource>Vendor_Module::custommodule</resource>
        <group id="custom" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Custom Options</label>
            <field id="customer_group_list" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Customer Groups</label>
                <source_model>Vendor\Module\Model\Adminhtml\System\Config\Source\Customer\Group</source_model>
            </field>
        </group>
    </section>
</system>
  1. Create Vendor\Module\Model\Adminhtml\System\Config\Source\Customer\Group.php file and add below code in it.
namespace Vendor\Module\Model\Adminhtml\System\Config\Source\Customer;

class Group implements \Magento\Framework\Option\ArrayInterface
{
protected $_options; 

public function __construct(\Magento\Customer\Model\ResourceModel\Group\CollectionFactory $groupCollectionFactory)
{
    $this->_groupCollectionFactory = $groupCollectionFactory;
}

/**
 * @return array
 */
public function toOptionArray()
{
    if (!$this->_options) {
        $this->_options = $this->_groupCollectionFactory->create()->loadData()->toOptionArray();
    }
    return $this->_options;
}
}

enter image description here

This item is saved as CSV, so in your Helper or Model you need to convert this to an array, with the following code:

        $enabledCustomerGroupsCsv = $this->scopeConfig
              ->getValue('custom_section/custom/customer_group_list');

        // If any groups have been configured
        if ($enabledCustomerGroupsCsv) {
            // Convert CSV into array
            $enabledCustomerGroupIds = explode(',', $enabledCustomerGroupsCsv);

            // Your code here
        }
1
  • 1
    This is exactly what I was looking for, thanks! For me it works on Magento 2.3.6-p1. I've added some steps to the answer on how to retrieve the configured value.
    – Jacques
    Apr 26, 2021 at 11:26
1

Without creating a new source_model

  1. Add the below code in MagePal/Test/etc/adminhtml/system.xml file.

    <?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="custom_section" translate="label" type="text" sortOrder="100" showInDefault="1" showInWebsite="1" showInStore="1">
         ...
         <group id="custom" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
             <label>Custom Options</label>
             <field id="customer_group_list" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
                 <label>Customer Groups</label>
                 <source_model>Magento\Customer\Model\Customer\Source\Group</source_model>
             </field>
         </group>
     </section>
    

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.