Skip to content

Instantly share code, notes, and snippets.

@druman
Created May 22, 2017 05:24
Show Gist options
  • Save druman/90da2b1bcd9036145aa4e4f68ca1b698 to your computer and use it in GitHub Desktop.
Save druman/90da2b1bcd9036145aa4e4f68ca1b698 to your computer and use it in GitHub Desktop.
Get display node id for a commerce product
/**
* Get display node id for a commerce product.
* @param $product_id
* An integer value of the product id.
* @param string $product_field_name
* Name of the commerce_product_reference field used to reference products from display node.
* @return FALSE|node_id
* Returns FALSE if no results or display node id on success.
*/
function MY_CUSTOM_MODULE_get_display_node_by_product_id($product_id, $product_field_name = 'product_reference') {
$query = new EntityFieldQuery;
$result = $query->entityCondition('entity_type', 'node', '=')
->propertyCondition('type', 'tire')
->fieldCondition($product_field_name, 'product_id', $product_id, '=')
->range(0, 1)->execute();
if (empty($result['node'])) {
return FALSE;
}
return reset($result['node'])->nid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment