Load all payment methods in system config
-
10-12-2020 - |
Question
I want to load all available payment methods in a multiselect system config in admin. Could some one help?
Solution
In system.xml
,
<test translate="label">
<label>Available Payments</label>
<frontend_type>select</frontend_type>
<source_model>module_name/system_config_source_payment</source_model>
<sort_order>5</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</test>
In Namespace/Modulename/Model/System/Config/Source/Payment.php
class Namespace_Modulename_Model_System_Config_Source_Payment
{
public function toOptionArray()
{
$payments = Mage::getSingleton('payment/config')->getActiveMethods();
//$methods = array(array('value'=>'', 'label'=>Mage::helper('adminhtml')->__('--Please Select--')));
foreach ($payments as $paymentCode=>$paymentModel) {
$paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
$methods[$paymentCode] = array(
'label' => $paymentTitle,
'value' => $paymentCode,
);
}
return $methods;
}
}
OTHER TIPS
Slight improvement on above answer - makes solution store specific.
class Namespace_Module_Model_System_Config_Source_Payment
{
public function toOptionArray()
{
//need to workout which store we are on
$methods = [];
$currentStoreId = null;
/* Get current configuration store code */
if(strlen($currentStoreCode = Mage::getSingleton('adminhtml/config_data')->getStore())){
$currentStoreId = Mage::getModel('core/store')->load($currentStoreCode)->getId();
}
$payments = Mage::getSingleton('payment/config')->getActiveMethods($currentStoreId);
foreach ($payments as $paymentCode=>$paymentModel) {
$paymentTitle = Mage::getStoreConfig('payment/'.$paymentCode.'/title');
$paymentTitle .= ' ('.$paymentCode.')';
$methods[$paymentCode] = array(
'label' => $paymentTitle,
'value' => $paymentCode,
);
}
return $methods;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange