[Drupal] How to create a custom Rule condition for Ubercart module?

| | 2 min read

Using Ubercart and the Drupal Rules modules, it is possible to alter the behavior of the Ubercart checkout process. We had came across a situation on one of our Drupal sites where we needed to add an additional feature during the checkout process. If you are faced with a similar scenario in your Drupal site with Ubercart then read on know how to create a custom Rule condition for the Ubercart module?

We needed to display the Tax rate in the checkout page during product purchase and the Tax rate was dependent on the currently selected state. For example, if the state of California was selected, then the Tax rate for the California state needs to be displayed inside the 'Payment pane' along with the Order total.

This can be done using the Rules module (with Ubercart installed) by adding a custom rule condition about which we have discussed below.

The Rules module provides a hook function for this process named 'hook_rules_condition_info()'. The structure of this hook will be like this:

function MYMODULE_rules_condition_info() {
  $conditions['custom_condition'] = array(
    'label' => t("Custom Title"),
    'group' => t('Custom Group'),
    'base' => 'MYMODULE_custom_function',
    'parameter' => array(
      'order' => array(
        'type' => 'uc_order',
        'label' => t('Order'),
      ),
    ),
  );	

return $conditions;	
}

The above function should be placed inside MYMODULE.rules.inc page in your custom module. In this function the 'base' will define a 'MYMODULE_custom_function' which performs the required action for that condition.

function  MYMODULE_custom_function() {
	//Perform the necessary actions.
}

So we have just mentioned above how a custom rule condition can be created using hook_rules_condition_info(). Hope this can be useful for someone.