0

Block->

<?php
namespace \Block;


use Magento\Payment\Api\PaymentMethodListInterface;

class AllPayment extends \Magento\Framework\View\Element\Template
{
    protected $_paymentConfig;
protected $_scopeConfigInterface;
 
public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Payment\Model\Config $paymentConfig,
 \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfigInterface,
 array $data = []
) 
{
 $this->_paymentConfig = $paymentConfig;
 $this->_scopeConfigInterface = $scopeConfigInterface;
 parent::__construct($context, $data);
}
 
public function getAllActivePaymentMethods() {
 $activePaymentMethods = $this->_paymentConfig->getActiveMethods();
 $activeMethods = array();
 if ($activePaymentMethods && count($activePaymentMethods) > 0) {
 foreach ($activePaymentMethods as $methodCode => $paymentModel) {
 $methodTitle = $this->_scopeConfigInterface->getValue('payment/' . $methodCode . '/title');
 $activeMethods[$methodCode] = array(
 'label' => $methodTitle,
 'value' => $methodCode
 );
 }
 }
 return $activeMethods;
}
}




phtml->

<?php
echo "<pre>";
print_r($allActivePaymentMethods);
echo "</pre>";

?>
11
  • But this is not work.. please answer and full code? Dec 29, 2022 at 12:01
  • go to the configuration check that your payment methods are enabled or not?
    – Mehran
    Dec 29, 2022 at 12:32
  • yes, 4 methods is enabled Dec 29, 2022 at 12:43
  • try my answer let me know if you face any issue?
    – Mehran
    Dec 29, 2022 at 12:47
  • foreach() argument must be of type array|object, null Dec 29, 2022 at 12:53

1 Answer 1

0

Block

<?php
namespace \Block;


use Magento\Payment\Api\PaymentMethodListInterface;

class AllPayment extends \Magento\Framework\View\Element\Template
{
/**
 * Order Payment
 *
 * @var \Magento\Sales\Model\ResourceModel\Order\Payment\Collection
 */
protected $_orderPayment;

/**
 * Payment Helper Data
 *
 * @var \Magento\Payment\Helper\Data
 */
protected $_paymentHelper;

/**
 * Payment Model Config
 *
 * @var \Magento\Payment\Model\Config
 */
protected $_paymentConfig;

/**
 * @param \Magento\Backend\Block\Template\Context $context
 * @param \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $orderPayment
 * @param \Magento\Payment\Helper\Data $paymentHelper
 * @param \Magento\Payment\Model\Config $paymentConfig
 * @param array $data
 */
public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Sales\Model\ResourceModel\Order\Payment\Collection $orderPayment,
    \Magento\Payment\Helper\Data $paymentHelper,
    \Magento\Payment\Model\Config $paymentConfig,
    array $data = []
) {
    $this->_orderPayment = $orderPayment;
    $this->_paymentHelper = $paymentHelper;
    $this->_paymentConfig = $paymentConfig;
    parent::__construct($context, $data);
}

/**
 * Get all payment methods
 * 
 * @return array
 */ 
public function getAllPaymentMethods() 
{
    return $this->_paymentHelper->getPaymentMethods();
}

/**
 * Get key-value pair of all payment methods
 * key = method code & value = method name
 * 
 * @return array
 */ 
public function getAllPaymentMethodsList() 
{
    return $this->_paymentHelper->getPaymentMethodList();
}

/**
 * Get active/enabled payment methods
 * 
 * @return array
 */ 
public function getActivePaymentMethods() 
{
    return $this->_paymentConfig->getActiveMethods();
}

/**
 * Get payment methods that have been used for orders
 * 
 * @return array
 */ 
public function getUsedPaymentMethods() 
{
    $collection = $this->_orderPayment;
    $collection->getSelect()->group('method');
    $paymentMethods[] = array('value' => '', 'label' => 'Any');
    foreach ($collection as $col) { 
        $paymentMethods[] = array('value' => $col->getMethod(), 'label' => $col->getAdditionalInformation()['method_title']);           
    }       
    return $paymentMethods;
}
}

Phtml

<?php
foreach($block->getActivePaymentMethods() as $paymentCode => $payment) {
echo "<pre>";print_r($payments);
}

foreach($block->getAllPaymentMethods() as $paymentCode => $payment) {
echo "<pre>";print_r($payments);
}
//by this way you can get the payment methods in phtml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.