3

I have a commerce product type (entity not display node) with two date fields (enable_date and disable_date). What are the steps to create a rule/component and use rules scheduler to schedule these to disable and enable these products, based on the value of these two fields?

The use case is that I have an event node (product display) that needs to always be viewable but the attached registration (product entity) needs to be disabled on a specific day/time. i.e. Registration is now closed, but users can still see the event info, like location, map etc. Using rules scheduler I am sure I can do this. I have tried with mixed results but I am still learning the rules module. Any help would be appreciated.

6
  • I deleted my response after you clarified that you aren't simply trying to remove products from your website so that customers couldn't see them (that is what my answer achieved). Clearly, you need to work with products, and not the product display for your needs. Mar 7, 2013 at 19:32
  • Thanks for you attempt Boriana, I should have worded my question better in the beginning. I guess I assumed using the terminology enable/disabled, rather than published/unpublished, meant I was working with commerce product entities and not the display nodes. I have upvoted one of your questions to make up for the downvote I gave you on your deleted answer. I will try to explain myself better on future questions. :) Mar 7, 2013 at 19:57
  • Oh, no problem at all, Nigel, please don't worry about that. Sometimes these things come out only from a discussion. I hope someone else can chime in here. I, myself, have the suspicion that it isn't trivial to loop through all existing entities of a type through rules (rather than just ones that are in someone's cart etc), but I hope someone else has figured it out! Mar 7, 2013 at 20:13
  • It won't have to loop through all existing entities. Trigger would be on product save and then it would schedule the action. I got part of the way there and get the schedule to work and the action I can create but I cannot seem to tie them together. It's probably because I don't fully understand rules components yet. Mar 7, 2013 at 20:36
  • Will you accept an answer that doesn't make use of Rules or Rules Scheduler? Is there a particular reason you are tied to it other than perceived efficiency? Mar 12, 2013 at 7:47

3 Answers 3

12
+300

First create a Rules component for setting the status of a product.

  1. Go to Configuration -> Workflow -> Rules -> Components -> Add new component.
  2. Create an Action set.
  3. Give the component a suitable name, such as Set product status.
  4. Create a parameter of type Commerce Product, label Product, machine name product; and another of type Truth value, label Status, machine name status.
  5. Add action: Set a data value
  6. For Data, use the Data selector to select the product parameter's status (product:status).
  7. For Value, use the Data selector to to select the status parameter (status).
  8. Save

You now have a component that can be scheduled. Next you need to trigger the creation of the scheduled action. I'm going to describe the process for enabling the product; you should be able to create another rule for disabling easily enough following the same method.

  1. Go to Configuration -> Workflow -> Rules -> Add new rule
  2. Choose the triggers After saving a new commerce product and After updating an existing commerce product.
  3. Add the condition Entity has field.
  4. For Entity, use the Data selector to choose commerce-product as the entity.
  5. For Field, use the dropdown to select your start date field.
  6. Add condition Data value is empty.
  7. For Data to check, use the Data selector to choose the start date field.
  8. Check the Negate checkbox (we only want to proceed if the field is not empty)
  9. Add another condition Data comparison and select the start date field is greater than site:current-date
  10. Add an action Schedule component evaluation.
  11. For Component, choose the component you created earlier.
  12. For Scheduled evaluation date, use the Data selector to get the start date.
  13. For Identifier, choose something like Enable product [commerce-product:product-id]
  14. For Status check the box.
  15. For Product use the Data selector to choose commerce-product.

Then in order to remove a scheduled action if the date's removed from the field:

  1. Follow steps 1-7 (but not 8!) immediately above to create a new rule.
  2. Add an action Delete scheduled tasks.
  3. For Component, choose the component you created earlier.
  4. For Identifer, use the same string you used before.

And another rule to delete the scheduled action when the product's deleted would be good too.

I quickly gave that a go and it seems to work for me. Let me know if you have any problems.

6
  • 1
    Awesome this is getting me closer. I added to your answer one more condition. #9 Add another condition Data comparison and select the start date field is greater than site:current-date. Otherwise it will still add a new scheduled item even if the date is in the past. One last missing pice is a way to delete the scheduled item if an admin changes their mind and removes a date from the product. The item is still scheduled. Any thoughts on how to overcome this? Mar 14, 2013 at 18:40
  • @NigelWaters Very good point, I hadn't thought of that. You can add a new rule with the same triggers that checks if the field is empty. If it is, it deletes the scheduled rule. I just did a quick check and there are no errors if you try to delete a non-existant scheduled action.
    – Andy
    Mar 14, 2013 at 18:57
  • 1
    Perfect that did it. I also added one more rule. After product is deleted, delete scheduled task. Mar 14, 2013 at 19:11
  • @NigelWaters I'll jus go and add that too, thanks!
    – Andy
    Mar 14, 2013 at 19:12
  • Thanks so much for your help @Andy. I really appreciate it. Upvoted and bounty awarded. :) Mar 14, 2013 at 19:19
1

Here is something I have done in the past and I think will help you retrieve your product ids without having to loop through every single product.

You can use VBO to load a list of entity IDs to your view that you can loop through. You can read how to do that here: http://nodeone.se/sv/node/777

This will allow you to create two views to return the entity ids for your products enabling today and disabling today.

Then create your RULE to fire during cron or something, as long as its once a day.

Add your Action to load a list of entity objects through a VBO display. Add a loop based on that loop parameter List. It defaults to "entity_list" Add an Action for the Loop to iterate over your entity list which is your product entity to either disable or enable the product. Select Data -> Set a value

Once you are in the set a value part you will see your "entity-list" data selector pop up and you can set the value for enabling or disabling to w/e you need it to be.

Hopefully this will get you where you need to be, let me know if you get stuck.

Just a slight followup, you can add both VBO views to the single rule. One VBO display pulls all the products that need to be enabled today that you loop through, and then you can create another action to pull the VBO display that turns off the products for today. So this will run every day turning on/off your products and you never have to worry about scheduling anything.

7
  • I have tens of thousands of products so looping through a view on every cron run is going to be overkill. I would much prefer to use rules scheduler. "After saving product entity, if product has field, compare system date, schedule product for enable/disable" or something like that. I am just unsure how to tie the rule with the component to create the scheduled rule. Mar 9, 2013 at 15:45
  • You would only loop through the products that are scheduled to turn on today, or turn off today. Will you have tens of thousands products that will turn on and off each day? When you setup your view, you add those options as a filter. So YOU ONLY retrieve the ones that require an action. Simple :)
    – iLLin
    Mar 11, 2013 at 15:31
  • I literally have thousands of products being enabled and disabled on an ongoing basis. It is an auction site (think ebay crossed with groupon) with a ton of sellers and buyers. The approach you recommend is great but I think leveraging the rules scheduler would be best in this scenario. Mar 11, 2013 at 20:29
  • Hmm if it is thousands, I would almost write a custom cron job and query where you can just UPDATE all records that have date of today to Enabled/Disabled. From a performance stand point, that would be way better.
    – iLLin
    Mar 12, 2013 at 14:28
  • Unfortunately the project does call for using rules scheduler. It's directly in the spec. I am just a subcontract in the project and must adhere to the rfp. Mar 12, 2013 at 14:57
0

I have also found a good answer of related to this question at Drupal groups. This is really nice discussion about creating rule in Drupal 7. Get the rest of material at https://groups.drupal.org/node/240678 .

1
  • This looks rather like a ink only answer. To avoid the risk of downvotes (or risk of it to get deleted), I would also include at least some sort of summary of what is included in that link ... Just a suggestion! Aug 13, 2016 at 15:55

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