0

I have a custom payment method in magento 2. I have added an enabled option in the payment settings page in the admin area, this is done in the same way as the payment methods from the offline payments module.

Currently this value affects nothing, you can always see the option for this payment method in the checkout even if enabled is set to no. Which piece of code makes this work for the offline payment options and how can I get my payment method to show or hide correctly?

0

Assuming that your module have the following structure and naming convention for the active field name you module should automatically get enabled/disabled. Since the module is enabled then the naming convention seem correct, so try flush cache or check to see if it is enabled on the store level.

config.xml

<system>
    <section id="payment">
        <group id="sample_gateway" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Sample Gateway</label>
            <field id="active" ....

See vendor/magento/module-payment/Model/Method/Adapter.php

/**
 * @inheritdoc
 */
public function isActive($storeId = null)
{
    return $this->getConfiguredValue('active', $storeId);
}
1
  • Your example config.xml matches what I have written in my system.xml, but still the module cannot be disabled. I presume this is just a typo. In my system.xml file, within the section with ID payment I have a group which contains a field with ID active. – DanJAB Nov 16 '16 at 13:19
0

I found two things that can cause this to happen.

The first thing to check is that the configuration option group in etc/adminhtml/system.xml matches the name of the payment method in etc/payment.xml

For example, your files might look like this:

etc/payment.xml

<?xml version="1.0"?>
<payment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Payment:etc/payment.xsd">
    <groups>
        <group id="mymethodgroup">
            <label>My payment group</label>
        </group>
    </groups>

    <methods>
        <!-- NOTE: method is called 'mymethodname' -->
        <method name="mymethodname">
            <allow_multiple_address>0</allow_multiple_address>
        </method>
    </methods>
</payment>

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="payment">
            <!-- NOTE the group id here matches the method name in etc/payment.xml -->
            <group id="mymethodname" translate="label" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>My payment method</label>
                <field id="active" translate="label" type="select" showInDefault="1" showInWebsite="1" showInStore="0">
                    <label>Enabled</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                ...

The second thing to check is that the etc/adminhtml/system.xml defines the active option in the correct place. Initially I had this inside a sub-group. This will not work. It must be at the top level. See the following example.

Example of a BAD 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="payment">
            <group id="mymethodname" translate="label" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>My payment method</label>
                <group id="basic" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Basic settings</label>
                        <!-- NOTE the active option is in a sub-group. This will not work -->
                        <field id="active" translate="label" type="select" showInDefault="1" showInWebsite="1" showInStore="0">
                            <label>Enabled</label>
                            <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                        </field>
                        ...
-1

Is absolutely right, if it is in a sub-group it won't work. But most of the time we would like to keep this option in a sub-group.

So basically we can keep as UI in a sub-group and set actual value in DB to not be in a sub-group, by adding <config_path></config_path> to your XML on etc/adminhtml/system.xml file

Like This :

<group id="your-sub-group" ....>
     <label>Advanced configuration </label>
      ...
     <field id="active" ....>
         <label>Enabled</label>
         <config_path>payment/YourModuleName/active</config_path>
         <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
     </field>
     ...
</group>

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.