diff --git a/modules/product_reference/commerce_product_reference.module b/modules/product_reference/commerce_product_reference.module index 25ae45b..588b1ee 100644 --- a/modules/product_reference/commerce_product_reference.module +++ b/modules/product_reference/commerce_product_reference.module @@ -346,29 +346,45 @@ function commerce_product_reference_entity_view($entity, $entity_type, $view_mod $wrapper = entity_metadata_wrapper($entity_type, $entity); } + // If the field is missing continue to next product reference field. + if (!isset($wrapper->{$field_name})) { + continue; + } + // Find the default product based on the cardinality of the field. $product = NULL; - if (isset($wrapper->{$field_name})) { - $product = $wrapper->{$field_name}->value(); - if ($field['cardinality'] != 1) { - // Store a reference to all the products. - $products = $product; - $product = commerce_product_reference_default_product($products); - - // If the product is disabled, attempt to find the next active one and - // use that as the default product instead. - if (empty($product->status)) { - foreach ($products as $product) { - if (!empty($product->status)) { - // Found an active product. - break; - } + $products = $wrapper->{$field_name}->value(); + + // If there are no products on this field continue to the next field. + if (empty($products)) { + continue; + } + + if ($field['cardinality'] == 1) { + // The value of single value reference fields is the product object. + $product = $products; + } + else { + // Find the default from the array of all the referenced products. + $default_product = commerce_product_reference_default_product($products); + + // If the product is disabled, attempt to find the next active one and + // use that as the default product instead. + if (!empty($default_product->status)) { + $product = $default_product; + } + else { + foreach ($products as $active_product) { + if (!empty($active_product->status)) { + // Found an active product. + $product = $active_product; + break; } } } } - // If we found a product and the reference field enables field injection... + // If we found a product and the reference field enables field injection. if (!empty($product) && $instances[$field_name]['settings']['field_injection']) { // Add the display context for these field to the product. $product->display_context = array(