Is it possible to show disabled payment methods only in admin while creating order from Admin / Sales / Orders and not in front end.
2 Answers
You can create a plugin to control which method you will show in the frontend, creating a plugin for:
Magento\Payment\Model\MethodList
Then you check the method by code and disabled which you want:
public function afterGetAvailableMethods(\Magento\Payment\Model\MethodList $subject, $result)
{
foreach ($result as $key=>$_result) {
if ($_result->getCode() == "cashondelivery") {
unset($result[$key]);
}
}
return $result;
}
In this way, you can show the payment method in the admin area only and disable it in the frontend area.
Try
Create a global event for payment_method_is_active
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="payment_method_is_active">
<observer name="mp_payment_restrictions" instance="MagePal\PaymentRestrictions\Observer\PaymentMethodActiveObserver" />
</event>
</config>
In MagePal/PaymentRestrictions/Observer/PaymentMethodActiveObserver.php
class PaymentMethodActiveObserver implements ObserverInterface
{
public function __construct(
..
) {
...
}
public function execute(Observer $observer)
{
$methodInstance = $observer->getMethodInstance();
$paymentCode = $methodInstance->getCode();
//todo: check if processing from backend or frontend
if ($result->getIsAvailable() && $paymentCode !== 'xyz') {
$result->setIsAvailable(false);
}
}
}
-
Thank you for your reply. This is not working. Still am getting only "Purchased Order" method nothing else. Commented Jul 7, 2022 at 16:17
-