Get Active Payment Method List in Frontend Custom Form | A Detailed Guide
Vinh Jacker | 11-11-2024
Providing various payment methods is crucial for customer convenience when completing orders in an online store. Relying on a single payment method isn’t sufficient. Customers prefer using their favorite payment options based on their location and preferences. As a result, this is how your list of active payment methods is increasing.
Custom development is often required if you need to display a list of active payment methods on the front end. Recently, many store owners wanted to allow their users to select a preferred default payment gateway during store signup.
Therefore, in this article, we will guide you to the best code that helps you to get active payment method list in frontend custom form of Magento 2. This will enable customers to easily choose their preferred payment gateway.
Step 1: Create a block
Firstly, create a block file named Paymentmethods.php
Add the following code to the file:
<?php
/**
* Mageplaza
*
* NOTICE OF LICENSE
*
* This source file is subject to the mageplaza.com license that is
* available through the world-wide-web at this URL:
* https://www.mageplaza.com/LICENSE.txt
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category Mageplaza
* @package Mageplaza_Customize
* @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
* @license https://www.mageplaza.com/LICENSE.txt
*/
namespace Mageplaza\Customize\Block;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Model\Config;
/**
* Class PaymentMethods
* @package Mageplaza\Customize\Block
*/
class PaymentMethods
{
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/**
* @var Config
*/
protected $paymentModelConfig;
/**
* PaymentMethods constructor.
*
* @param ScopeConfigInterface $scopeConfig
* @param Config $paymentModelConfig
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
Config $paymentModelConfig
) {
$this->paymentModelConfig = $paymentModelConfig;
$this->scopeConfig = $scopeConfig;
}
/**
* @return array
*/
public function getActivePaymentMethod()
{
$payments = $this->paymentModelConfig->getActiveMethods();
$methods = array();
foreach ($payments as $paymentCode => $paymentModel)
{
$paymentTitle = $this->scopeConfig->getValue('payment/'.$paymentCode.'/title');
$methods[$paymentCode] = [
'label' => $paymentTitle,
'value' => $paymentCode
];
}
return $methods;
}
Step 2: Use the function in the PHTML file
Now, navigate to your PHTML
file where you want to call this function and add this shortcode.
<div>
<h1>Active Payment Methods: </h1>
<?php
$paymentMethods = $block->getActivePaymentMethod();
foreach ($paymentMethods as $method) : ?>
<span><strong>Name:</strong> <?= $method['label'] ?></span> -
<span><strong>Code:</strong> <?= $method['value'] ?></span><br>
<?php endforeach; ?>
</div>
The Result
This is the result you will get on the frontend when you follow the above steps:
Wrap up
By implementing the above steps, you’ll be able to retrieve and showcase the active payment methods in your Magento 2 store’s frontend custom form.
If you have any problems while following this tutorial, feel free to let us know.
Happy coding!