Get a display (referencing) node id for a commerce product

Public

This is a generic function that will return a display node id for a given product. Source: http://www.drupalcommerce.org/questions/3176/how-get-node-id-product-dis...

</> CopyGet raw version
php
  1. <?php
  2. function MODULE_NAME_get_referencing_node_id($product) {
  3. // Itterate through all fields which refer to commerce products.
  4. foreach (commerce_info_fields('commerce_product_reference') as $field) {
  5. // Build query.
  6. $query = new EntityFieldQuery;
  7. $query->entityCondition('entity_type', 'node', '=')
  8. ->fieldCondition($field['field_name'], 'product_id', $product->product_id, '=')
  9. ->range(0, 1);
  10.  
  11. if ($result = $query->execute()) {
  12. // Return node id.
  13. return array_shift(array_keys($result['node']));
  14. }
  15. }
  16.  
  17. return false;
  18. }

Comments

Anonymous's picture

Here's an easier way:

  1. field_product_node->value();
  2. $product_display = array_shift($nodes);