As per the requirement, we can add dynamic payment methods list with multi select in the system configuration in admin.
For this, first we need to create system.xml file – it is located in directory Magemonkeys/Payment/etc/adminhtml/system.xml where we need to add below code.
<field id="payment_methods" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1"
showInStore="1">
<label>Sku</label>
<frontend_model>MagemonkeysPaymentBlockSystemConfigFormFieldFields</frontend_model>
<backend_model>MagentoConfigModelConfigBackendSerializedArraySerialized</backend_model>
<comment>Dynamic Payment methods list</comment>
</field>
Second thing we need to do is to create Magemonkeys/Payment/Block/System/Config/Form/Field/Fields.php to render multi select field in system configuration.
<?php
declare(strict_types=1);
namespace MagemonkeysPaymentBlockSystemConfigFormField;
use MagentoConfigBlockSystemConfigFormFieldFieldArrayAbstractFieldArray;
use MagemonkeysPaymentBlockAdminhtmlFormFieldPaymentSkuColumn;
use MagentoFrameworkDataObject;
use MagentoFrameworkExceptionLocalizedException;
class Fields extends AbstractFieldArray
{
/**
* @var bool
*/
protected $_addAfter = false;
/**
* @var
*/
protected $_addButtonLabel;
/**
* Rows cache
*
* @var array|null
*/
private $_arrayRowsCache;
/**
* @var TaxColumn
*/
private $paymentRenderer;
/**
* Construct
*/
protected function _construct()
{
parent::_construct();
$this->_addButtonLabel = __('Add');
}
/**
* Prepare to render the columns
*/
protected function _prepareToRender()
{
$label = 'label';
$this->addColumn('sku', [$label => __('Sku')]);
$this->addColumn('payment_method', [
'label' => __('Payment Method'),
'renderer' => $this->getPaymentRenderer(),
'extra_params' => 'multiple="multiple"'
]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');
}
/**
* Prepare existing row data object
*
* @param DataObject $row
* @throws LocalizedException
*/
protected function _prepareArrayRow(DataObject $row): void
{
$options = [];
$payment = $row->getPaymentMethod();
if ($payment !== null) {
foreach ($payment as $country) {
$options['option_' . $this->getPaymentRenderer()->calcOptionHash($country)] = 'selected="selected"';
}
}
$row->setData('option_extra_attrs', $options);
}
/**
* @return TaxColumn
* @throws LocalizedException
*/
private function getPaymentRenderer()
{
if (!$this->paymentRenderer) {
$this->paymentRenderer = $this->getLayout()->createBlock(
PaymentSkuColumn::class,
'',
['data' => ['is_render_to_js_template' => true]]
);
}
return $this->paymentRenderer;
}
}
Then, we need to create Magemonkeys/Payment/Block/Adminhtml/Form/Field/PaymentSkuColumn.php where file returns payment methods list.
<?php
declare(strict_types=1);
namespace MagemonkeysPaymentBlockAdminhtmlFormField;
use MagentoPaymentHelperData;
use MagentoFrameworkViewElementContext;
use MagentoFrameworkViewElementHtmlSelect;
class PaymentSkuColumn extends Select
{
private $paymentHelper;
public function __construct(
Context $context,
Data $paymentHelper,
array $data = []
) {
parent::__construct($context, $data);
$this->paymentHelper = $paymentHelper;
}
public function setInputName($value)
{
return $this->setName($value . '[]');
}
public function _toHtml(): string
{
$options = [];
$paymentMethod = $this->paymentHelper->getPaymentMethods();
foreach ($paymentMethod as $key => $payment) {
if(isset($payment['title'])){
$options[] = ['label' => $payment['title'], 'value' => $key];
}else{
$options[] = ['label' => $key, 'value' => $key];
}
}
if (!$this->getOptions()) {
$this->setOptions($options);
}
$this->setExtraParams('multiple="multiple"');
return parent::_toHtml();
}
}
It will look something like this.


