I`m trying to alter product price with hook_node_presave.
So I`m loading products attached to my product display.
And it seems that both commerce_product_load and commerce_product_load_multiple cause this error:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\AppServ\www\trailhead\includes\entity.inc).

Here an example how I`m trying to do it:

$pids = array();
foreach ($node->field_product['und'] as $key => $product_id) {
  if ( !empty($product_id) ) {
    $pids[] = $product_id;
  }
}
if ( !empty($pids) ) {
  $products = commerce_product_load_multiple($pids);
}

or like this:

foreach ($node->field_product['und'] as $key => $product_id) {
  if ( !empty($product_id) ) {
    $product = commerce_product_load($product_id);
  }
}

Both variants work properly but return set of errors. Can`t find out how to fix this errors.

Comments

vasike’s picture

Category: bug » support
Status: Active » Fixed

your issue is real, But:
1. there's no bug.
2. the code is wrong - $node->field_product['und'] array items are arrays not product ids : array('product_id' => xxx), where is xxx is the product id number

here is the corrected code:

foreach ($node->field_product['und'] as $key => $product_array) {
  if ( !empty($product_array) ) {
    $product = commerce_product_load($product_array['product_id']);
  }
} 
lanzs’s picture

Status: Fixed » Closed (fixed)

Oh, such a stupid error, I was looking everythere but not in that place (because I was sure of using product_id).

Thaks a lot for help!