is there anyway, we can disable payment of specific payment option via custom module? I Have done some research that payment in drupal commerce is setup via rules, so is there an option we can create custom module and hit the current payment rules and disable it?

and is there a way via custom module, we can get the list of available or activated payment?

some references would be great.

thank you!

  • payment methods are rules, so it can be disabled from rules configuration, why do you need custom module for that
    – Shabir A.
    Jan 18, 2016 at 13:33
  • I need to disable the payment on certain days but this will be annually, as rules can not receive token of years in field I tested
    – Boby
    Jan 18, 2016 at 15:00

1 Answer 1


Option 1: Hook Form Alter

Rules are event-based conditional actions. For "showing" or "hiding" payments, that event it almost always going to be simply showing the form on checkout. So, technically, you could just use hook_form_alter() and locate the checkout pane that has the payment details and modify from there.

A video where I walk you through how to do hook_form_alter: https://youtu.be/xYMd6WDqxKY?t=1m3s (admittedly, this is for a different use case, but I do show you how to locate the form_id and customize the form_alter for whatever situation you're in).

Option 2: Custom Rules Condition

Another option would be to create a rule condition that you can add to your payment gateways. I think this would be more in spirit with how Drupal Commerce works.

Over here, I have shown you how to create a custom Rules action and condition: Different vat rate per commerce shipping service

There is also a clear example here: How do I add a new condition in Rules 2?

How to do option 2

As I said above, this is how Drupal Commerce is supposed to work. If you create a custom rule condition (perhaps there's a business server that returns a REST true/false for an A/B comparison) then that condition is exposed in the user interface and keeps the logic in code where it can be maintained separate from being implemented.

  1. Create the custom rule condition in your custom module with your custom logic
  2. Add that condition to your payment method: /admin/commerce/config/payment-methods
  3. Know that every time that payment method might appear (both in checkout and on the order editing payment screen) that rule condition will be run
  • I wanted to do a custom module, as rules does not allow me to insert any [token] in the data comparison of the date field. as I need to run this rules annually and the way to achieve that is to use custom module, so at the current date every year, the payment will be off/disabled. that is something I am looking to prepare..
    – Boby
    Jan 18, 2016 at 15:08
  • Yes, the custom rule condition is in a custom module, where your logic can take advantage of any contextual entities and return a true/false. Thus, option 2 is a custom module approach (that happens to integrate with the rules system). ... Otherwise you can do option 1 and just use hook_form_alter.
    – joshmiller
    Jan 18, 2016 at 15:26

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.