diff --git a/modules/product_reference/commerce_product_reference.module b/modules/product_reference/commerce_product_reference.module index 7210a58..79df4d8 100644 --- a/modules/product_reference/commerce_product_reference.module +++ b/modules/product_reference/commerce_product_reference.module @@ -60,15 +60,40 @@ function commerce_product_reference_field_extra_fields() { } } - // Do the same for fields on products that may be visible on the bundle. - foreach (field_info_instances('commerce_product') as $product_bundle_name => $product_fields) { - foreach ($product_fields as $product_field_name => $product_field) { - $extra[$entity_type][$bundle_name]['display']['product:' . $product_field_name] = array( - 'label' => t('Product: @label', array('@label' => $product_field['label'])), - 'description' => t('Field from a referenced product.'), - 'weight' => $product_field['widget']['weight'], - 'configurable' => TRUE, - ); + // Referenceable product types for the field instance + $product_types = array(); + $field_instance = field_info_instance($entity_type, $field_name, $bundle_name); + $referenceable_types = $field_instance['settings']['referenceable_types']; + foreach ($referenceable_types as $key => $referenceable_type) { + if ($referenceable_type) { + $product_types[] = $referenceable_type; + } + } + // Limit to the fields of Referenced product types + if (!empty($product_types)) { + foreach ($product_types as $product_type) { + // Do the same for fields on products that may be visible on the bundle. + foreach (field_info_instances('commerce_product', $product_type) as $product_field_name => $product_field) { + $extra[$entity_type][$bundle_name]['display']['product:' . $product_field_name] = array( + 'label' => t('Product: @label', array('@label' => $product_field['label'])), + 'description' => t('Field from a referenced product.'), + 'weight' => $product_field['widget']['weight'], + 'configurable' => TRUE, + ); + } + } + } + else { + // No product types specified to be referenced. Add all the fields. + foreach (field_info_instances('commerce_product') as $product_bundle_name => $product_fields) { + foreach ($product_fields as $product_field_name => $product_field) { + $extra[$entity_type][$bundle_name]['display']['product:' . $product_field_name] = array( + 'label' => t('Product: @label', array('@label' => $product_field['label'])), + 'description' => t('Field from a referenced product.'), + 'weight' => $product_field['widget']['weight'], + 'configurable' => TRUE, + ); + } } } }