I am trying add a commerce product to a node when a person fills out a content node. I am generating the product, but can't add it back to the node.

When I try to add the data to the node, there is no product reference field data when Do not show a widget is selected in the content type > Manage fields.

Do not show a widget seems to remove the field from the node completely.

If I try to load in hook_node_presave():

$product = commerce_product_load_by_sku($sku);

$field_studio_pass_product = array(
  LANGUAGE_NONE => array(
    0 => array(
      'product_id' => $product->product_id,

$node->field_studio_pass_product = $field_studio_pass_product;

It won't work since there is no current field. I don't want to show the field to editor as they can't change it.

Further. This above code does work if the product field has a widget, or the ideal code:

$node_wrapper = entity_metadata_wrapper('node', $node);

Again, if you set the field type to Do not show a widget there is no property on the node object, and as a result no ability to add a value.

2 Answers 2


Your issue isn't that there wasn't a field available in the data model to edit but that you used the wrong array structure for the product reference. Instead of the "value" key you should've been using "product_id".

I highly recommend against directly inserting field data like your answer proposes. The Entity / Field APIs should be allowed to perform their full jobs on this data when it's entered. For example, in your case you didn't put the necessary data into the field data revision table at the same time.

  • Agreed with concept, answer doesn't work though. There is no field_studio_pass_product property of the node array. This means that I can't add it. See drupal.stackexchange.com/questions/12290/… for updating a node field - it is normally 'value'. If a stdClass does not have a property the only way I know to add properties is to cast to array and back - another hack. I tried a few other such as altering the field to make it hidden.
    – Interlated
    Commented Jan 10, 2017 at 2:56
  • Yes. If you use entity_metadata_wrapper you get an array that has field_studio_pass_product['und'][0]['product_id'] = 56. Doesn't make it work under the 'no widget' scenario.
    – Interlated
    Commented Jan 10, 2017 at 3:13
  • Could be the order of base entity vs. field hook invocation then. If you're just going to make direct queries on node insert you may as well actually update / save the entity itself. Commented Jan 11, 2017 at 3:51

The following 'nice answer' works

$node_wrapped = entity_metadata_wrapper('node', $node);
$node_wrapped->field_studio_pass_product = $product->product_id;
field_attach_update('node', $node);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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