0

I want to display only those in this payment method filter drop down which are enabled on store front. if i had 4 payment methos enables on store front then only four options should be shown in this payment method drop down filter in sales order grid

enter image description here

This is my approach

Vendor/MultiselectFilters/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Payment\Ui\Component\Listing\Column\Method\Options">
        <plugin name="Mageplaza_HelloWorld_Plugin" type="Vendor\Module\Plugin\ActiveMethod" sortOrder="10" disabled="false"  />
    </type>
</config>

\Vendor\Module\Plugin\ActiveMethod.php

<?php
namespace Vendor\Module\Plugin;

use Magento\Payment\Model\Config;
use Magento\Payment\Ui\Component\Listing\Column\Method\Options;

class ActiveMethod
{
    /**
     * Payment Model Config
     *
     * @var Config
     */
    protected Config $paymentConfig;

    /**
     * @param Config $paymentConfig
     */
    public function __construct(
        Config $paymentConfig
    ) {
        $this->paymentConfig = $paymentConfig;
    }

    public function afterToOptionArray(
        Options $subject,
        $result
    ) {
        $activeMethods[] = $this->paymentConfig->getActiveMethods();
        return [$activeMethods];

    }
}

after this code i get empty drop down as in this picture enter image description here

but when i hit debugger i get two objects in array enter image description here Thanks in advance. any idea how to do this?

1 Answer 1

0

It's a 2 way, if you want to collect enabled payment for all websites, you can use following helper

app/code/Acme/StackExchange/Helper/Payment.php

<?php
declare(strict_types=1);

namespace Acme\StackExchange\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreManagerInterface;

class Payment extends AbstractHelper
{
    protected StoreManagerInterface $storeManager;

    public function __construct(
        Context $context,
        StoreManagerInterface $storeManager
    ) {
        $this->storeManager = $storeManager;

        parent::__construct($context);
    }

    public function getPaymentMethodOptions(int $websiteId = null, bool $includeFree = true): array
    {
        $options = [];
        foreach ($this->scopeConfig->getValue('payment') as $code => $data) {
            if (!isset($data['model'], $data['title'])) {
                continue;
            }
            if ($code === 'free' && !$includeFree) {
                continue;
            }
            $title  = $data['title'];
            $active = $data['active'] ?? false;
            if ($websiteId) {
                $active = $this->scopeConfig->isSetFlag(
                    sprintf('payment/%s/active', $code),
                    ScopeInterface::SCOPE_WEBSITE,
                    $websiteId
                );
                $title  = (string)$this->scopeConfig->getValue(
                    sprintf('payment/%s/title', $code),
                    ScopeInterface::SCOPE_WEBSITE,
                    $websiteId
                );
            } else {
                foreach ($this->storeManager->getWebsites(false) as $website) {
                    $isActive = $this->scopeConfig->isSetFlag(
                        sprintf('payment/%s/active', $code),
                        ScopeInterface::SCOPE_WEBSITE,
                        $website->getId()
                    );
                    if ($isActive) {
                        $active = true;
                        $title  = $this->scopeConfig->getValue(
                            sprintf('payment/%s/title', $code),
                            ScopeInterface::SCOPE_WEBSITE,
                            $website->getId()
                        );
                    }
                }
            }
            if ($active) {
                $options[] = [
                    'value' => $code,
                    'title' => $title,
                ];
            }
        }

        return $options;
    }
}

or you can use Magento\Payment\Model\Config and format options in expected format

$options = [];
foreach ($this->paymentConfig->getActiveMethods() as $code => $instance) {
    $options[] = [
        'value' => $code,
        'label' => $instance->getTitle(),
    ];
}

return $options;

I suggest to create separate source model and overwrite it to use instead of create plugin to exists source in your case

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.