Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

Get Active Payment Method List in Frontend Custom Form | A Detailed Guide

Vinh Jacker | 11-11-2024

Get Active Payment Method List in Frontend Custom Form | A Detailed Guide

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:

Active payment method list in frontend custom form

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!

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services