How to Get All Payment Methods in Magento 2
Gone are the days where most people preferred to pay and accept cash. Online transactions have become the prime medium for payments. After all, the center point of the whole shopping universe is money!
Tons of different options are available for accepting payments online and it is crucial and a critical task for eCommerce to select the right type of payment method.
Magento 2 supports a large number of payment methods internally and externally. To develop a full-fledged Magento 2 store, it should be able to accept payments from customers.
You can allow customers to pay using below payment methods in Magento 2, such as:
-
- Check/money order
- Cash on delivery
- Bank Transfer
- Zero Subtotal Checkout
- Purchase Order
These mentioned ways are default payment methods that can be configured from Admin -> Stores -> Configuration -> Sales -> Payment Methods. Out of the box, Magento 2 supports several external payment methods too. Therefore, if you have more than one store with different payment methods, it will become complicated to work with payments.
Let’s take a scenario where you are developing an extension to avail admin select a particular payment method to set validation. To serve the purpose of allowing selection of the payment methods, you require to get all payment methods in Magento 2. Likewise you can also make a payment method visible only to admin in your store to restrict access to certain payments methods and make them visible only to admin. Also you can get payment details from order in Magento 2 to extract and access the payment information for specific products.
Once you load the list of all payment methods in Magento 2 configuration, the admin can select all or some of them to set validation on. Below are the programmatic steps to get all payment methods in Magento 2.
Steps to Get All Payment Methods in Magento 2.
- Create Data.php file and use this code.
1234567891011121314151617181920212223242526272829303132333435<?phpnamespace Meetanshi\Module\Helper;use Magento\Framework\App\Helper\AbstractHelper;use Magento\Framework\App\Helper\Context;use Magento\Payment\Helper\Data as paymentData;class Data extends AbstractHelper{protected $paymentHelper;public function __construct(Context $context,paymentData $paymentHelper){$this->paymentHelper = $paymentHelper;parent::__construct($context);}public function getAllPaymentMethods(){// payment methods with detail$allPaymentMethods = $this->paymentHelper->getPaymentMethods();// payment methods list array$allPaymentMethodsArray = $this->paymentHelper->getPaymentMethodList();$this->_logger->info(print_r($allPaymentMethods, true));$this->_logger->info(print_r($allPaymentMethodsArray, true));return $allPaymentMethodsArray;}}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950Array([free] => Array([active] => 1[model] => Magento\Payment\Model\Method\Free[order_status] => pending[title] => No Payment Information Required[payment_action] => authorize_capture[allowspecific] => 0[sort_order] => 1[group] => offline)[checkmo] => Array([active] => 1[model] => Magento\OfflinePayments\Model\Checkmo[order_status] => pending[title] => Check / Money order[allowspecific] => 0[group] => offline)[purchaseorder] => Array([active] => 0[model] => Magento\OfflinePayments\Model\Purchaseorder[order_status] => pending[title] => Purchase Order[allowspecific] => 0[group] => offline)[banktransfer] => Array([active] => 0[model] => Magento\OfflinePayments\Model\Banktransfer[order_status] => pending[title] => Bank Transfer Payment[allowspecific] => 0[group] => offline)[cashondelivery] => Array([active] => 0[model] => Magento\OfflinePayments\Model\Cashondelivery[order_status] => pending[title] => Cash On Delivery[allowspecific] => 0[group] => offline))
12345678Array([banktransfer] => Bank Transfer Payment[cashondelivery] => Cash On Delivery[checkmo] => Check / Money order[free] => No Payment Information Required[purchaseorder] => Purchase Order)If you have any difficulties regarding this blog, do consider them posting in the Comments section below.
I’m here help.
Feel free to share the solution with Magento 2 community via social media.
Thank You.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
How to Install Varnish Cache on Windows
How to Update Product Attribute Value Programmatically in Magento 2
Next