5

How can I get the list of payment methods that can use in the following?

$quote->setPaymentMethod('checkmo'); 

I copied it from a sample online and I guess it is Check / Money Order.

1

2 Answers 2

9

Or you can just..

Inject dependency.

\Magento\Framework\App\Config\ScopeConfigInterface $scope

Get value

$methodList     = $this->scope->getValue('payment');

Iterate over.

foreach( $methodList as $code => $_method )
{
    something...
}
7

Try to use payment helper:

Magento\Payment\Helper\Data::getPaymentMethods()

Add Magento\Payment\Helper\Data to the __construct method:

/**
 * @var \Magento\Payment\Helper\Data
 */
protected $paymentHelper;

/**
 * @param \Magento\Payment\Helper\Data $paymentHelper
 */
public function __construct(
    \Magento\Payment\Helper\Data $paymentHelper
) {
    $this->paymentHelper = $paymentHelper;
}

Then use it to get all payment methods like this:

$this->paymentHelper->getPaymentMethods();

or:

$this->paymentHelper->getPaymentMethodList();

Here is this methods:

/**
 * Retrieve all payment methods
 *
 * @return array
 */
public function getPaymentMethods()
{
    return $this->_initialConfig->getData('default')[self::XML_PATH_PAYMENT_METHODS];
}

/**
 * Retrieve all payment methods list as an array
 *
 * Possible output:
 * 1) assoc array as <code> => <title>
 * 2) array of array('label' => <title>, 'value' => <code>)
 * 3) array of array(
 *                 array('value' => <code>, 'label' => <title>),
 *                 array('value' => array(
 *                     'value' => array(array(<code1> => <title1>, <code2> =>...),
 *                     'label' => <group name>
 *                 )),
 *                 array('value' => <code>, 'label' => <title>),
 *                 ...
 *             )
 *
 * @param bool $sorted
 * @param bool $asLabelValue
 * @param bool $withGroups
 * @param Store|null $store
 * @return array
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function getPaymentMethodList($sorted = true, $asLabelValue = false, $withGroups = false, $store = null)
{
    $methods = [];
    $groups = [];
    $groupRelations = [];

    foreach ($this->getPaymentMethods() as $code => $data) {
        if (isset($data['title'])) {
            $methods[$code] = $data['title'];
        } else {
            $methods[$code] = $this->getMethodInstance($code)->getConfigData('title', $store);
        }
        if ($asLabelValue && $withGroups && isset($data['group'])) {
            $groupRelations[$code] = $data['group'];
        }
    }
    if ($asLabelValue && $withGroups) {
        $groups = $this->_paymentConfig->getGroups();
        foreach ($groups as $code => $title) {
            $methods[$code] = $title;
        }
    }
    if ($sorted) {
        asort($methods);
    }
    if ($asLabelValue) {
        $labelValues = [];
        foreach ($methods as $code => $title) {
            $labelValues[$code] = [];
        }
        foreach ($methods as $code => $title) {
            if (isset($groups[$code])) {
                $labelValues[$code]['label'] = $title;
            } 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;
    }

    return $methods;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.