Search Results :

×

Disable a Payment Method Programmatically in Magento 2

To disable a payment method programmatically in Magento 2, you can use the following steps:

    Step 1:

    First, you need to create a new PHP file in your Magento 2 project's root directory or in any custom module's directory.

    Step 2:

    In that file, add the following code to get an instance of the payment configuration model:
    
    use Magento\Payment\Model\Config; class DisablePaymentMethod { protected $paymentConfig; public function __construct(
    Config $paymentConfig ) { $this->paymentConfig = $paymentConfig; } public function disablePaymentMethod($methodCode) {
    $this->paymentConfig->setValue( 'active', 0, 'payment', $methodCode ); } }
              


    Step 3:

    In the above code, we have injected the Magento\Payment\Model\Config class in the constructor, and then defined the disablePaymentMethod method that takes the payment method code as an argument.

    Step 4:

    In the disablePaymentMethod method, we have called the setValue method of the payment configuration model to set the active value to 0 for the given payment method code

    Step 5:

    Now, you can call the disablePaymentMethod method by passing the payment method code to disable that payment method programmatically, like this:
    $disablePayment = new DisablePaymentMethod($paymentConfig); $disablePayment->disablePaymentMethod('payment_method_code');
                

    Step 6:

    After executing the above code, the payment method with the given code will be disabled programmatically.

    Note: Make sure to replace payment_method_code with the actual payment method code that you want to disable.

1000+ Organizations That Trust Us



Contact Us


 Thank you for your response. We will get back to you soon.

Something went wrong. Please submit your query again

Hello there!

Need Help? We are right here!

support
Contact miniOrange Support
success

Thanks for your inquiry.

If you dont hear from us within 24 hours, please feel free to send a follow up email to info@xecurify.com