Zend certified PHP/Magento developer

Write Magento2 Custom Module to set Minimum Order Amount for Selected Customer Groups

I developed a Magento 2 module to add customer groups dropdown in admin panel Store-> Configuration -> Sales -> Minimum Order Amount so I can set min order amount only for some customer groups and not for all as you can see in the picture, the customer group dropdown is added in the admin panel config and I can select some. So far so good!

enter image description here

The issue is when I select the customer groups and save the config, the min order amount still applies to all customers groups and the de-selected customer groups still have the order amount restriction.

I didn’t write any observer in my module and I don’t know how; but it seems I should use after_save observer to tell Magento 2 that saves the selected customer groups along with other config as well!

Here is my custom module. Please let me know if any file is missing and help me to set min order amount only for selected customer groups:

registration.php

<?php
MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'CustomApi_OrderMinimum',
    __DIR__
);

etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="CustomApi_OrderMinimum" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
            <module name="Magento_Sales"/>
        </sequence>
    </module>
</config>

etc/di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="CustomApiOrderMinimumModelConfigSourceCustomerGroups">
        <arguments>
            <argument name="groupRepository" xsi:type="object">MagentoCustomerApiGroupRepositoryInterface</argument>
            <argument name="searchCriteriaBuilder" xsi:type="object">MagentoFrameworkApiSearchCriteriaBuilder</argument>
        </arguments>
    </type>
</config>

etc/config.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <sales>
            <minimum_order>
                <minimum_order_amount>
                    <enabled>1</enabled>
                    <customer_groups>
                        <!-- Define customer group IDs where minimum order applies -->
                        <group1>0</group1>
                        <group2>1</group2>
                        <group1>2</group1>
                        <group2>3</group2>
                        <group1>4</group1>
                        <group2>5</group2>
                        <group1>6</group1>
                        <group2>7</group2>
                        <group1>8</group1>
                        <group2>9</group2>
                        <!-- Add more groups as needed -->
                    </customer_groups>
                </minimum_order_amount>
            </minimum_order>
        </sales>
    </default>
</config>

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="sales" translate="label" type="text" sortOrder="200" showInDefault="1" showInWebsite="1" showInStore="1">
            <group id="minimum_order" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Minimum Order Amount</label>
            
                <field id="customer_groups" translate="label comment" type="multiselect" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Customer Groups</label>
                    <source_model>CustomApiOrderMinimumModelConfigSourceCustomerGroups</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Model/Config/Source/CustomerGroups.php:

<?php
namespace CustomApiOrderMinimumModelConfigSource;

use MagentoFrameworkOptionArrayInterface;
use MagentoFrameworkApiSearchCriteriaBuilder;
use MagentoCustomerApiGroupRepositoryInterface;

class CustomerGroups implements ArrayInterface
{
    protected $groupRepository;
    protected $searchCriteriaBuilder;

    public function __construct(
        GroupRepositoryInterface $groupRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->groupRepository = $groupRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    public function toOptionArray()
    {
        $options = [];
        $searchCriteria = $this->searchCriteriaBuilder->create();
        $groups = $this->groupRepository->getList($searchCriteria);

        foreach ($groups->getItems() as $group) {
            $options[] = [
                'value' => $group->getId(),
                'label' => $group->getCode(),
            ];
        }

        return $options;
    }
}

Thank you all.