0

I have created a custom offline payment method, which I want to be visible through API, but not during web checkout.

I tried with <can_use_checkout>0</can_use_checkout> in config.xml, but it affects API as well.

I have found the source of the payment list in this file, vendor/magento/module-checkout/view/frontend/web/template/payment-methods/list.html , but unsure how to hide a specific payment method from here.

5
  • you want to hide it for frontend only ?
    – Pawan
    Jul 18, 2023 at 10:58
  • @Pawan Yes, it would appear everywhere else. Just hidden for the customer placing order using web. Jul 18, 2023 at 11:12
  • create a plugin for getAvailableMethods. refer github.com/pawan/adminenablepayments/blob/master/Plugin/Model/…
    – Pawan
    Jul 19, 2023 at 4:34
  • @Pawan Won't this disable the method from API calls as well ? Jul 19, 2023 at 8:10
  • it shouldn't if you put di.xml in front end folder
    – Pawan
    Jul 19, 2023 at 8:44

1 Answer 1

0

Vendor/Module/etc/events.xml

<event name="payment_method_is_active">
     <observer name="disable_paymentMethods" instance="Vendor\Module\Observer\ObserverforDisabledFrontendPaymentMethods" />
</event>

Vendor\Module\Observer\ObserverforDisabledFrontendPaymentMethods.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\App\State;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\LocalizedException;

class ObserverforDisabledFrontendPaymentMethods implements ObserverInterface
{
    /**
     * @var State
     */
    protected $_appState;

    /**
     * ObserverDisabledFrontendPg constructor.
     * @param State $appState
     */
    public function __construct(
        State $appState
    ) {
        $this->_appState = $appState;
    }

    /**
     * @param Observer $observer
     * @throws LocalizedException
     */
    public function execute(Observer $observer)
    {
        $result = $observer->getEvent()->getResult();
        $methodInstance = $observer->getEvent()->getMethodInstance();
        $quote = $observer->getEvent()->getQuote();

        if (null !== $quote) {
            $methodCode = $methodInstance->getCode();
            //Chnage with your method name
            if ($methodCode == "payment_name" && $this->_appState->getAreaCode() == "frontend")
            {
                $result->setData('is_available', false);
            }
        }
    }
}
8
  • Thank you for the quick response. I will try this out. Jul 18, 2023 at 11:13
  • This didn't work unfortunately. The custom payment method is getting removed from V1/carts/mine/shipping-information as well. Jul 18, 2023 at 11:27
  • Print the AreaCode when you use the API. Jul 18, 2023 at 13:17
  • check which area code you get when execute the API Jul 18, 2023 at 13:17
  • I tried , and for both cases (API and frontend) it shows webapi_rest Jul 19, 2023 at 8:08

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.