Prevent Payment Methods According To The Conditions

Let’s Begin with the new thing today. The most common or I would say, one of the most important too topic of Magento 2. Every Store flow in Magento has to go through this. so we can say it’s a common path for all.

Before started working on the Payment Prevention method on the checkout page. Let’s have a look at why we need this method or functionality.

Example: if someone wants to hide some active Payment method for a few SKUs or for some attribute or may be for some cart subtotal, then by default Magento does not provide such functionality by which we can adjust payment methods. So, for this, we will need to do some customizations.

Let begin this step by step.

Let’s start with the Basic Module Structure First. Here I assume one should already Have Basic knowledge about Module and its structure, if not please refer to this link first.

I have created an Excellence_Preventpayment Module.

So, we first have to put a validation that prevents the active payment authentication on the payment section of checkout page.

To do this we need to use observer which will validate the payment method according to the condition

Excellence/Preventpayment/etc/events.xml

As we have added our custom observer PaymentMethodAvailable.php at Excellence\Preventpayment\Observer\PaymentMethodAvailable

So, now when a customer proceeds to the payment page this observer will get a call and hence will validate the payment methods as per our condition here.

So, now the most common scenario here which I am not going to implement as it depends upon the developer how he/she fetch the conditional data which we will implement on this observer.

Here, I am setting data into the store config from where Admin can select the payment method which he/she wants to disable on the payment page.

colorpicker

After this, will be fetching the selected payment methods in our observer with the help of observer and comparing these payments with the active payment methods on the store. so, those active payment methods match without the selected payment method will get hide on the payment page.

In the above code, I have checked that the module is enabled or not, then proceeded with rest of the functionalities to fetch the data and apply condition accordingly. Hence on the Payment page, now the selected methods would be disabled even if a particular payment method is enabled from admin panel.

So, That’s all folks.

I have given a small scenario that how we can implement this on the frontend, still one can utilize this with different scenarios and can also make it much better by implementing more functionalities according to their need.