3

Anyone knows how to set an expiration date on a Drupal 7 Rule (using the Rules module) so that the rule is disabled on a certain date?

Example Use Case:

We've created a product (using Drupal Commerce) with a name that's just a long string of random upper and lowercase characters and numbers. We then created a rule with one condition and two actions so that when Drupal is initialized (the event) it recognizes a specific URL a user came to the site with (ie. sitename/?SKU=productnamewithlotsofcharacters)[using the text comparison condition and then the provide URL argument value action] and when that's the case it re-directs the user to a specific product form (page redirect action to a drupal commerce express checkout form).

We only want this promo to last until a certain date, but we will be using it again in the future (so we just want the rules disabled). When the user uses this link to get to our site and the promo is 'expired' it takes them to a page that says 'Promo Expired'.

It's not the most fool-proof method of doing things of course but it works well enough for our needs.

  • Although, this suggestion does not disable your rule, you could add a date condition to your rule so it does not trigger the action after x date. – No Sssweat Mar 29 '16 at 8:05
  • Why do you want to disable your view after a certain date? is it disabled forever? do you mind sharing more details of your use case? – No Sssweat Mar 29 '16 at 8:06
  • I've added context to my question, thanks for asking! Just add a date condition? I don't see the date condition...maybe it's not attached by default to the Drupal Initialize event? – Sage Mar 31 '16 at 6:52
4

Programmatically disable a rule contains an example of how you can disable a rule programatically, as in this example:

/**
* Implements hook_update_N().
* Disables name_of_rule rule.
*/
function hook_update_N() {
  $rules_config = rules_config_load('name_of_rule');
  $rules_config->active = FALSE;
  $rules_config->save();
}

Tune this example to fit the machine name of your rule to be disabled, and then use that as the "PHP code " in a Rules Component (Action) which only performs an "Execute custom PHP code".

With that, the only challenge left to make this work, is to enhance your rule to be disabled, to conditionally trigger this (new) Rules Component "after the moment has arrived" for your rule to be disabled. For this you might want to add "Conditional Rules" (so that you can add a condition within the Rules Action of your rule to be disabled).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.