2

I'm using Commerce module and I have a line item with a computed field that's calculating a date. This field finally shows a date.

Then during the checkout process I have a form with a Date field (part of the Order). I'm running some Rules and finally I have to copy one of the calculated dates to this field but i couldn't by rules.

I tried with a simple rule "set a data value" trying to copy the Computed field value to the other field. I put a "has field" with the CF. Then in actions, the field doesn't appear in the data selector.

This is normal? Is there another method to do it?

Many Thanks!!

0

You may want to give it a try to use the NOT commonly known/used alternative available for the Computed Field module, i.e. the (fairly new) Math Field module (for D7). Some more details about this module (from its project page), which also details when the calculations actually happen:

The Math Field module defines a new field type to dynamically calculate values on entity forms. This is an alternative to Computed Field that does not require the use of PHP Filter.

Math expression fields are dynamically updated via ajax on the entity form as soon as all necessary fields are populated. If JavaScript is disabled, the field will be evaluated when the form is submitted. The result is stored in the database when the form is submitted.

For more details (step-by-step instructions), refer to either of these links:

Known issues

As this is a fairly new D7 module, there are still a few (known) issues with it, as mentioned also on its project page:

Bonus: you would not need the "PHP filter" (IMO only that should be a valid reason to investigate this alternative) ...

  • Thanks Pierre but the problem is I'm using CF to calculate a date. For what a saw this is not possible with this module. – Nacho Jun 21 '16 at 13:29
  • @Nacho merci for the feedback. That's not what I read in your question. But can you try to give some more details? Also, adding a Rules condition like "entity has field" should usually help (as you already did). What happens if you'd use "content is of type" instead? – Pierre.Vriens Jun 21 '16 at 13:38
  • Hi Pierre, sorry for the delay. I changed the question, added more information. Hope that help. I tried what you suggested but the problem persist. Thanks. – Nacho Jun 29 '16 at 15:23
  • @Nacho Since you seem to be using multiple rules, can you think of adapting 2 of them so that in a 1st rule you store the field you cannot copy (yet) and then in a 2nd rule you try to retrieve that field (instead of trying to copy it)? – Pierre.Vriens Jun 29 '16 at 15:30
  • How to store the computer field? because as i said before if i use a rule like "set a data value" the problem is the CF doesn't appear in the value select box in spite of that a rule "Entity has field" already exist. – Nacho Jun 30 '16 at 18:58
0

Anyone running into this recently, I found a work around.

I used "Entity has field" as a condition on the Computed Field Value.

I then used the "Add a variable" action to add a text value of the Computed Field as a variable.

Then added a "Convert Data Type" action to turn the value into an integer.

I could then use "Set a data value" as normal.

Here is an export of my rule (for a different use case), but still setting Computed Field values as a Variable...

{ "rules_set_default_remaining_balance" : {
    "LABEL" : "Set Default Remaining Balance",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "entityform" ],
    "ON" : { "entityform_insert" : [] },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "entityform" ], "field" : "field_total_price" } },
      { "entity_has_field" : { "entity" : [ "entityform" ], "field" : "field_remaining_balance" } },
      { "data_is_empty" : { "data" : [ "entityform:field-remaining-balance" ] } }
    ],
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : [ "entityform:field-total-price" ] },
          "PROVIDE" : { "variable_added" : { "variable_total_price" : "Total Price" } }
        }
      },
      { "data_convert" : {
          "USING" : { "type" : "integer", "value" : [ "variable-total-price" ] },
          "PROVIDE" : { "conversion_result" : { "conversion_result" : "Conversion result" } }
        }
      },
      { "data_set" : {
          "data" : [ "entityform:field-remaining-balance" ],
          "value" : [ "conversion-result" ]
        }
      }
    ]
  }
}

In the example above, the field "Total Balance" is being Computed.

  • Hi, i tried but didn't work for me. The CF i have in the product (also i tried to put it in the line item) and used in "Entity has field", doesn't appear in the "Add a variable" rule. Do you have some special settings for the CF? – Nacho Aug 4 '16 at 14:06
  • I have added my exported rule to the answer if that helps? – Collins Aug 4 '16 at 14:16

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.