Skip to content

Commit

Permalink
use a custom source model to have only active payment methods
Browse files Browse the repository at this point in the history
  • Loading branch information
agranjeon committed May 28, 2019
1 parent 04f0c2a commit 545d7af
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
23 changes: 0 additions & 23 deletions Model/Config/Source/Locale.php

This file was deleted.

79 changes: 79 additions & 0 deletions Model/Config/Source/Payment/AllActiveMethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Agranjeon\Faker\Model\Config\Source\Payment;

/**
* @author Alexandre Granjeon <[email protected]>
*/
class AllActiveMethods implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var \Magento\Payment\Helper\Data
*/
protected $_paymentData;
/**
* @var \Magento\Payment\Model\Config $_paymentConfig
*/
protected $_paymentConfig;

/**
* AllActiveMethods constructor
*
* @param \Magento\Payment\Helper\Data $paymentData
* @param \Magento\Payment\Model\Config $paymentConfig
*/
public function __construct(\Magento\Payment\Helper\Data $paymentData, \Magento\Payment\Model\Config $paymentConfig)
{
$this->_paymentData = $paymentData;
$this->_paymentConfig = $paymentConfig;
}

/**
* {@inheritdoc}
*/
public function toOptionArray()
{
$methods = [];
$groupRelations = [];

foreach ($this->_paymentData->getPaymentMethods() as $code => $paymentMethod) {
if (!isset($paymentMethod['active']) || $paymentMethod['active'] != 1) {
continue;
}
if (isset($paymentMethod['title'])) {
$methods[$code] = $paymentMethod['title'];
} else {
$methods[$code] = $this->_paymentData->getMethodInstance($code)->getConfigData('title');
}
if (isset($paymentMethod['group'])) {
$groupRelations[$code] = $paymentMethod['group'];
}
}
$groups = $this->_paymentConfig->getGroups();
foreach ($groups as $code => $title) {
$methods[$code] = $title;
}
asort($methods);
$labelValues = [];
foreach ($methods as $code => $title) {
$labelValues[$code] = [];
}
foreach ($methods as $code => $title) {
if (isset($groups[$code])) {
$labelValues[$code]['label'] = $title;
if (!isset($labelValues[$code]['value'])) {
$labelValues[$code]['value'] = null;
}
} elseif (isset($groupRelations[$code])) {
unset($labelValues[$code]);
$labelValues[$groupRelations[$code]]['value'][$code] = ['value' => $code, 'label' => $title];
} else {
$labelValues[$code] = ['value' => $code, 'label' => $title];
}
}

return $labelValues;
}
}
2 changes: 1 addition & 1 deletion etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
</field>
<field id="payment_method" translate="label" type="multiselect" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Payment method</label>
<source_model>Magento\Payment\Model\Config\Source\Allmethods</source_model>
<source_model>Agranjeon\Faker\Model\Config\Source\Payment\AllActiveMethods</source_model>
</field>
<field id="shipping_method" translate="label" type="multiselect" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Shipping method</label>
Expand Down

0 comments on commit 545d7af

Please sign in to comment.