[Drupal] Configuring conditions in rules module in Drupal 7

| | 2 min read

Using Drupal 7.x contributed Rules module we can write conditional actions on events. Conditions can be simple or complex. Here we are going to discuss how to write or configure rules conditional action on event validate commerce payment discount coupon.

If we have to validate a commerce payment discount coupon by its expire date, then say we have to check the order has the entity percentage/fixed coupon. If entity exists then check its expire date. Thing to be remembered while writing conditions for rule action is, we can't write 'OR', 'AND' and 'NOT' conditional operators as in normal programming language such as PHP. It is very difficult to express in word how to implement condition. Refer the screen shot of condition part in rules configuration page and then I will explain the way of configuration.
Rule_Action_condition_img1.png

Explanation: We have check whether the order has entity coupon which may be fixed/percentage type. If coupon exists, check for expire date of coupon in this case field will be field_coupon_fixed_expire_date if the type is of fixed and field_coupon_percentage_expire_date if the type is of percentage. In either case we have compare its expire date with current date. For more clarification I am replicating the condition in PHP script language (just for reference).

if((isset($coupon->field_coupon_fixed_expire_date) &&   
    $coupon->field_coupon_fixed_expire_date > date("Y-m-d 00:00:00")) || 
    (isset($coupon->field_coupon_percentage_expire_date) &&
    $coupon->field_coupon_percentage_expire_date > date("Y-m-d 00:00:00"))) {
    // Valid coupon
  }

Similarly we can implement Negate/Not condition for Drupal 7.x Rules condition.