Creating commerce product display in rules with PHP results in error This problem is to do with the following modules:


Drupal Commerce
Rules
Entity


I have the following code in an "Execute custom PHP code" Rules action. It creates a product display to go with a product (also created by Rules).

The problem is, it does not populate the product reference field (field_product_reference) and gives an error.

Code:

$product = $contact_details_product;
$display = new stdClass();
$display-&gt;type = 'product_display_regular';
node_object_prepare($display);

$display-&gt;title  = "Product display";
$display-&gt;language = LANGUAGE_NONE;

$display-&gt;field_product_reference['und'][0]['product_id'] = $product-&gt;product_id;
echo "TYPE: " . gettype($product-&gt;product_id);
node_save($display);


Where:


$contact_details_product is a variable available in Rules due to the previously created 'product' (as opposed to the display). This product was created in the same rule by a previous action.
product_display_regular is the product display content type.
field_product_reference is the machine name of the product reference field.


Error:

Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController-&gt;load() (line 178 of /home/test/public_html/includes/entity.inc).


Hacking entity.inc to display some values showed that at some point the variable on line 178 gets a null value, so I'm guessing there's a problem with the php code in the rule.
Output of gettype and print_r in entity.inc:

TYPE: string
Array ( [0] =&gt; 170 )
TYPE: string
Array ( [0] =&gt; 170 )
TYPE: NULL
Array ( [0] =&gt; )
...


That's about all I have right now. Can give more info if needed.
Cheers!
On further investigation it seems that the product created by Rules (in the previous action) is not assigning a product_id in the first place. Therefore there is nothing to assign to the 'product display'.

(I still don't understand why this is happening but rather than change this question completely I will post a new question.)