Configuring Multi-Select Active Payment Methods in Magento 2 Backend - magento source24


Configuring Multi-Select Active Payment Methods in Magento 2 Backend :- 

Hello magento friends in this blog post i'm share with you to Configuring Multi-Select Active Payment Methods in Magento 2 Backend.In Magento 2, you can retrieve active payment methods with multi-select functionality in the backend using a combination of backend models, collections, and configuration classes. Here's a step-by-step guide on how to achieve this:

➤ Create a Configuration File:

In your custom module, create a system.xml configuration file in the etc/adminhtml directory. This file will define the backend configuration fields for selecting active payment methods.

<!-- File: app/code/YourVendor/YourModule/etc/adminhtml/system.xml -->

<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="active_methods" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">

                <label>Active Payment Methods</label>

                <field id="methods" translate="label" type="multiselect" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">

                    <label>Select Active Payment Methods</label>

                    <source_model>YourVendor\YourModule\Model\Config\Source\PaymentMethods</source_model>

                </field>

            </group>

        </section>

    </system>

</config>

 

➤ Create Source Model:

Now, create a source model that will provide the list of active payment methods for the multiselect field.

// File: app/code/YourVendor/YourModule/Model/Config/Source/PaymentMethods.php

namespace YourVendor\YourModule\Model\Config\Source;

use Magento\Framework\Option\ArrayInterface;

use Magento\Payment\Model\Config;

class PaymentMethods implements ArrayInterface

{

    protected $paymentConfig;

    public function __construct(Config $paymentConfig)

    {

        $this->paymentConfig = $paymentConfig;

    }

    public function toOptionArray()

    {

        $methods = $this->paymentConfig->getActiveMethods();

        $options = [];

        foreach ($methods as $code => $method) {

            $options[] = [

                'value' => $code,

                'label' => $method->getTitle(),

            ];

        }

        return $options;

    }

}

 

Configuring Multi-Select Active Payment Methods in Magento 2 Backend - magento source24

➤ Access Active Payment Methods:

With the above setup, you have defined a configuration field in the backend that allows selecting active payment methods. Now you can access the selected methods using the configuration value.

// Get active payment methods selected in backend configuration

$selectedMethods = $this->scopeConfig->getValue('payment/active_methods/methods', ScopeInterface::SCOPE_STORE);

// $selectedMethods will contain an array of selected payment method codes

In this case Remember to replace YourVendor and YourModule with your actual vendor and module names. After implementing these steps, you should be able to configure and retrieve active payment methods with multi-select functionality in the Magento 2 backend.


Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post