1

I want to get all active payment method list store wise.

I have two store

-- store indstore

-- store usstore

And i have set payment methods for only indstore and if i try to get all active methods ,it returns from default store settings and from default settings payment methods are disable.

if i enable from default settings than i got all payment methods.

Does anyone know how i can get methods store wise?

app\code\Vendor\Extension\Model\System\Methods.php

<?php

namespace Vendor\Extension\Model\System;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Payment\Model\Config;
class Methods implements \Magento\Framework\Option\ArrayInterface
{
    protected $scopeConfig;
    protected $paymentmodelconfig;

    public function __construct(Config $paymentmodelconfig, ScopeConfigInterface $scopeConfig)
    {
        $this->paymentmodelconfig = $paymentmodelconfig;
        $this->scopeConfig = $scopeConfig;
    }

    public function toOptionArray()
    {
        $payments = $this->paymentmodelconfig->getActiveMethods();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);

        $methodList = $this->scopeConfig->getValue('payment',\Magento\Store\Model\ScopeInterface::SCOPE_STORE,1);

            foreach ($methodList as $code => $_method) {
                $active_status = "";
                $title = "";
                if (isset($_method['active']))
                {
                    if ($_method['active'] == 1)
                    {
                        if (isset($_method['title']))
                        {
                            $title = $_method['title'];
                            $logger->info($title);
                        }
                    }
                }
                 $methods = array();
                 return $methods;
        }
    }
}

Output IN Log File

2019-12-24T05:55:20+00:00 INFO (6): Check / Money order

Paypal is also active but i am getting only check/money order

  • Thanks @RohanHapani But using above link i got payment method as per default settings,but i want store wise – Sanjay Shiyal Dec 24 '19 at 5:12
  • You can just set it as : $this->scopeConfig->getValue('payment',\Magento\Store\Model\ScopeInterface::SCOPE_STORE,'store_code'); – Rohan Hapani Dec 24 '19 at 5:16
  • Let me know if you have still confusion about that. – Rohan Hapani Dec 24 '19 at 5:18
  • @RohanHapani i have tried as per you suggest but still getting same output , i have paypal payment method enable for only storeind but i m not gettting that payment method – Sanjay Shiyal Dec 24 '19 at 5:46
  • Update your full code in question which you have tried. – Rohan Hapani Dec 24 '19 at 5:52
3
class ABC{
    protected $paymentMethodList;
    public function __construct(
        \Magento\Payment\Model\PaymentMethodList $paymentMethodList
    ) {
        $this->paymentMethodList = $paymentMethodList;
    }

    public function getmethods()
    {
        $storeId = 'your store id';
        $this->paymentMethodList->getActiveList($storeId);
    }
}
| improve this answer | |
  • 1
    Hello @Padhiyar Gaurang Working Great !!! – Sanjay Shiyal Dec 24 '19 at 6:49
  • @SanjayShiyal felling good that it helps!! Cheers – Padhiyar Gaurang Dec 24 '19 at 6:49
0
namespace VendorName\ModuleName\Model;

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Magento\Payment\Model\Config;

class Paymentmethod extends \Magento\Framework\DataObject 
    implements \Magento\Framework\Option\ArrayInterface
{
    /**
     * @var ScopeConfigInterface
     */
    protected $_appConfigScopeConfigInterface;
    /**
     * @var Config
     */
    protected $_paymentModelConfig;

    /**
     * @param ScopeConfigInterface $appConfigScopeConfigInterface
     * @param Config               $paymentModelConfig
     */
    public function __construct(
        ScopeConfigInterface $appConfigScopeConfigInterface,
        Config $paymentModelConfig
    ) {

        $this->_appConfigScopeConfigInterface = $appConfigScopeConfigInterface;
        $this->_paymentModelConfig = $paymentModelConfig;
    }

    public function toOptionArray()
    {
        $payments = $this->_paymentModelConfig->getActiveMethods();
        $methods = array();
        foreach ($payments as $paymentCode => $paymentModel) {
            $paymentTitle = $this->_appConfigScopeConfigInterface
                ->getValue('payment/'.$paymentCode.'/title');
            $methods[$paymentCode] = array(
                'label' => $paymentTitle,
                'value' => $paymentCode
            );
        }
        return $methods;
    }
}

Here $_appConfigScopeConfigInterface is an object of Magento\Framework\App\Config\ScopeConfigInterface class.

$_paymentModelConfig is an object of Magento\Payment\Model\Config class.

and in toOptionArray() function we use a method: getActiveMethods() which returns model of all the active methods.

In return array we have all payment methods with their code and title.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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