In this post I am going to explain how to get all payment methods in magento 2.
1. Using Dependency Injection
Add below code snippet in Block class.
protected $_paymentHelper;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Payment\Helper\Data $paymentHelper,
array $data = []
)
{
$this->_paymentHelper = $paymentHelper;
parent::__construct($context, $data);
}
public function getAllPaymentMethods()
{
$paymentMethods = $this->_paymentHelper->getPaymentMethods();
return $paymentMethods;
}
Add below code snippet in template file.
//get all payment methods $allPaymentMethods = $block->getAllPaymentMethods(); echo "<pre>"; print_r($allPaymentMethods); echo "</pre>";
2. Using Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$paymentHelper = $objectManager->get('Magento\Payment\Helper\Data');
//get all payment methods
$allPaymentMethods = $paymentHelper->getPaymentMethods();
echo "<pre>";
print_r($allPaymentMethods);
echo "</pre>";Thats it. Enjoy Magento 2!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!


Leave a Reply