Magento 2: Get Payment Methods (All/Active/Used)Magento

Magento 2: Get Payment Methods (All/Active/Used)Magento

October 1, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2: Get Payment Methods (All/Active/Used)Magento

Magento 2: Retrieving Payment Methods

In Magento 2, retrieving payment methods is a crucial aspect of ensuring your store’s checkout process runs smoothly. Whether you need to fetch all available payment methods, active ones, or those that have been used in orders, this guide will walk you through the process with practical examples.

Getting All Payment Methods

To retrieve all payment methods in your Magento 2 store, you can use the Magento\Payment\Model\Config class. Here’s a simple way to achieve this:

use Magento\Payment\Model\Config;

$config = $objectManager->create('Magento\Payment\Model\Config');
$paymentMethods = $config->getActiveMethods();

In this code snippet, $objectManager->create() is used to instantiate the Magento\Payment\Model\Config class. The getActiveMethods() method returns an array of all active payment methods.

Retrieving Active Payment Methods

If you specifically want only the active or enabled payment methods, you can still use the same code as above. By default, the getActiveMethods() method filters out inactive methods.

$activePaymentMethods = $config->getActiveMethods();

This will give you an array containing all payment methods that are currently set to “Enabled” in your Magento admin panel.

Getting Payment Methods Used in Orders

To fetch payment methods that have been used while placing orders, you’ll need to query the sales_order_payment table. This involves a bit more database interaction than the previous examples.

$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('sales_order_payment');

$query = "SELECT distinct method FROM {$tableName}";

$usedPaymentMethods = $connection->fetchAll($query);

In this example, the Sales_Order_Payment table is queried to get a list of unique payment methods that have been used in orders. The $connection->fetchAll() function returns an array of all rows matching the query.

Best Practices and Tips

  1. Use the Object Manager Cautiously: In production code, avoid using the objectManager directly for dependency injection like shown in examples above. Instead, use Magento’s built-in dependency injection system provided by the framework.

  2. Optimize Queries: If your store has a large number of orders and you’re dealing with a significant amount of data, consider optimizing your database queries to improve performance.

  3. Avoid Hardcoding Table Names: It’s generally a good practice to use Magento’s built-in table name resolution functions rather than hardcoding table names in your queries.

Conclusion

Retrieving payment methods in Magento 2 is straightforward once you understand the available options. Whether you need all, active, or used payment methods, the Magento\Payment\Model\Config class and direct database queries provide efficient solutions.

Remember to optimize your code and follow best practices for dependency injection and performance optimization as your store grows. For a more detailed exploration of Magento 2 development, consider checking out Laravel Company’s Magento 2 Development Guide.

This guide covered the basics of retrieving payment methods in Magento 2. With these techniques, you can build robust checkout processes and enhance your store’s functionality.

Happy coding!

Magento 2 Checkout Database Admin

Our network