3

I've got a rule which fires on "Customer completes checkout".

When it fires I would like to update a field in a node which is related to the one which was "checked out".

The node which is "checked out" has an entity reference field which relates it to another node, of a different content type.

So I have my Rule Event: "Customer completes checkout" And the condition: "Entity has field" which checks for the entity reference field

Under Actions, I have: "Fetch entity by ID" to fetch the referenced node. And I have provided a "Variable name" for the Fetched Entity.

I know this much works because I can print the Title of the referenced node as a status message on the site at this point. I can even print the contents of the field that I want to update using the token [the-fetched-node:field-name]

But when I add an Action: "Set Data value" and look for the field to update, the-fecthed-node:field-name is not available.

I can see things like the-fetched-node:body and the-fetched-node:title, but none of the fields of the node seem to be available to Set Data values on...

How can I get access to set the data in my required field?

I'm using the Rules (7.x-2.9) and Ubercart (7.x-3.9) modules.

2

This question seemms to be a variation of Populate an entity reference field, which suggests a solution like so:

  • Add Condition: “Entity has field”, checking for your reference field
  • Add Action: “Fetch entity by id”, supplying your node/user id from another field in the dataset and providing a meaningful name (Note: This solution implies you get the id by a simple field value in your dataset; you might want to add another condition to register the id first)
  • Add Action: “Set data value”, which you now can supply your entity object from the step before (hence a name you'll identifiy in the dataset)

Apart from some Rules "Event" you probably need to add to it (related to your "After customer completes checkout"), this should be pretty close to what you're looking for.

  • Thanks @pierre.vriens but my question is quite different from the example you linked to. I need to "fetch" an entity reference (I can do that) then fetch the node it references (this is where I am stuck), which is a different content type, and then update a field in the referenced node (I'm pretty sure that will be easy once I have the node!) But atm I can't work out the required actions / conditions to pull in the node which needs updating. I'm now thinking I may need a loop...? – olisb Jan 6 '16 at 0:19
1

Under the hood, Rules 7.x-2.x keeps around something called a RulesState object, which you can think of as a sort of grab bag that keeps data around for conditions and actions of the rule to process later in the chain, if the Rules event you're triggering on does not make the data you want directly available.

@pierre.vriens' approach is not too bad. Somehow, you need to get grab the field you want so the field is available (i.e., stamped into RuleState) for the action you want to use.

I'm guessing the event you're trapping is defined as:

  $events['uc_checkout_complete'] = array(
    'label' => t('Customer completes checkout'),
    'group' => t('Cart'),
    'variables' => array(
      'order' => array(
        'type' => 'uc_order',
        'label' => t('Order'),
      ),
    ),
  );

The definition means you will get a uc_order object, named as "order" as part of your RulesState. This is set up as an entity in uc_order.module.

I haven't used Ubercart, but from reading the code, it looks like the uc_order object has a "products" array, and that the "bid" node you want to access will be in that array. So:

  • As the first part of the actions, do a Rules loop that uses the "products" array to pull out one of the products, and assign it to a variable via a data selector.
  • At this point, the bid should be accessible, along with its fields. If all you need to do is update the content of another of the node's fields, it should now be possible.

If you can have multiple products in the cart's order but only want to operate on some one them, it gets a little trickier, although I think you can create a Rule Component that you will call for each of the items in the cart. The component can test if the component is of the "bid" type, and process just those. Since components can bundle conditions along with actions, this should give you the flexibility you need.

  • Thanks - I think I am getting there... I have updated the question to make it clearer where I have got to! – olisb Feb 11 '16 at 2:34
  • Didn't know about that "grab bag" you mentioned. Any links you can recommend to learn more about that? – Pierre.Vriens Feb 16 '16 at 7:28
1

Create a component

Plugin: Rule

enter image description here

Component Name: kewl

Data Type: Node >> Name: kewl >> Machine name: kewl

enter image description here

Add Condition >> Entity has field >> kewl >> [select the field you want to update]

Add Action >> Set a data value >> kewl:[the field you want to update]

Create Rule

Add Loop >> node:field-[your reference field name]

  • Add Action >> Under Components, Rule: Kewl >> list-item
  • thanks - this looks like it might work, but I'm stuck. In Components I don't have an option for Data Type: "Node", I only see things like: Date, Duration, Flag etc and then lots of Entity options and List options. Which should I choose? – olisb Feb 12 '16 at 18:26
  • @olisb updated answer with screenshots. – No Sssweat Feb 13 '16 at 2:15
  • thanks No Ssswaet :) Unfortunately I don't have Entity > Node in my menu - perhaps I need another module to make it show up!? see my screenshot: defactodesign.com/sites/default/files/… – olisb Feb 13 '16 at 21:07
  • on simplytest.me/project/rules selecting the 7.x.2.9 option, login, enable rules UI. Add component, Node shows up. So no, there shouldn't be a need for another module. I then installed ubercart and enabled all ubercart modules and node still shows up. So idk why it's not showing up for you. – No Sssweat Feb 13 '16 at 23:21
  • very strange - thanks for all your help. I also logged in and tested it at simplytest.me and I do see the Entity > node option, but not on my setup :( I updated my Drupal to the latest version 7.34 but still no joy. Perhaps Entity - node is missing because of a conflict with another module? Or perhaps I could use something else, like Entity - Content? – olisb Feb 25 '16 at 15:41

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.