0

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->type = 'product_display_regular';
node_object_prepare($display);

$display->title  = "Product display";
$display->language = LANGUAGE_NONE;

$display->field_product_reference['und'][0]['product_id'] = $product->product_id;
echo "TYPE: " . gettype($product->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->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] => 170 )
TYPE: string
Array ( [0] => 170 )
TYPE: NULL
Array ( [0] => )
...

That's about all I have right now. Can give more info if needed. Cheers!

0

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.)

0

This work-around will do what you need using Drupal 7 database abstraction layer: This code is from a module I wrote to create product displays from an XML feed:

  $title_result = db_query('SELECT title FROM {commerce_product} WHERE product_id = :product_id', array(':product_id' => $prodid))->fetchField(); 
$node = new stdClass();
$node->title = $title_result;
$node->type = 'product_display';
node_object_prepare($node);

$node->language = LANGUAGE_NONE;
$node->uid = $user->uid; 
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 1; // 0 = comments disabled, 1 = read only, 2 = read/write
$node->field_product['und'][0]['product_id'] = $prodid;
$node = node_submit($node); // Prepare node for saving
node_save($node);
drupal_set_message( "Product Display for " . $prodid . " created.\n");

Obviously some variables are in my code that are not accounted for but hopefully it will be clear what I did.

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.