From 942d7368a835462ce30bdc9ca8bd4ab376acb787 Mon Sep 17 00:00:00 2001
From: Maciej Zgadzaj <maciej.zgadzaj@gmail.com>
Date: Mon, 10 Dec 2012 05:51:28 -0500
Subject: [PATCH] Issue #1861170 by maciej.zgadzaj: Option to specify a default product from multiple referenced products

---
 modules/cart/commerce_cart.module                  |    3 +-
 .../commerce_product_reference.api.php             |   31 ++++++++++++++++++++
 .../commerce_product_reference.module              |   19 +++++++++++-
 3 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 modules/product_reference/commerce_product_reference.api.php

diff --git a/modules/cart/commerce_cart.module b/modules/cart/commerce_cart.module
index 074a8cd..4115ea0 100644
--- a/modules/cart/commerce_cart.module
+++ b/modules/cart/commerce_cart.module
@@ -2269,7 +2269,8 @@ function commerce_cart_field_formatter_view($entity_type, $entity, $field, $inst
     // Check to ensure products are referenced, before returning results.
     if (!empty($products)) {
       $type = !empty($settings['line_item_type']) ? $settings['line_item_type'] : 'product';
-      $line_item = commerce_product_line_item_new(reset($products), $settings['default_quantity'], 0, array(), $type);
+      $default_product = commerce_product_reference_default_product($products);
+      $line_item = commerce_product_line_item_new($default_product, $settings['default_quantity'], 0, array(), $type);
       $line_item->data['context']['product_ids'] = array_keys($products);
       $line_item->data['context']['add_to_cart_combine'] = $settings['combine'];
       $line_item->data['context']['show_single_product_attributes'] = $settings['show_single_product_attributes'];
diff --git a/modules/product_reference/commerce_product_reference.api.php b/modules/product_reference/commerce_product_reference.api.php
new file mode 100644
index 0000000..0580dc2
--- /dev/null
+++ b/modules/product_reference/commerce_product_reference.api.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * @file
+ * This file contains no working PHP code; it exists to provide additional
+ * documentation for doxygen as well as to document hooks in the standard
+ * Drupal manner.
+ */
+
+/**
+ * Allows modules to alter the default product used by Product reference fields.
+ *
+ * By default Product reference fields use the first product from all products
+ * referenced by such field as a default one. This hook allows you to change
+ * this behavior and set any other of available $products as a default one
+ * by changing its $default_product_delta value.
+ *
+ * @param int
+ *   Delta of a default product from provided product entities array.
+ * @param array
+ *   Array of Commerce product entities.
+ *
+ * @see commerce_product_reference_default_product()
+ */
+function hook_commerce_product_reference_default_product_alter(&$default_product_delta, $products) {
+  foreach ($products as $delta => $product) {
+    if ($product->sku == 'product1') {
+      $default_product_delta = $delta;
+    }
+  }
+}
diff --git a/modules/product_reference/commerce_product_reference.module b/modules/product_reference/commerce_product_reference.module
index 62cb8b8..64535a1 100644
--- a/modules/product_reference/commerce_product_reference.module
+++ b/modules/product_reference/commerce_product_reference.module
@@ -322,7 +322,7 @@ function commerce_product_reference_entity_view($entity, $entity_type, $view_mod
         $product = $wrapper->{$field_name}->value();
       }
       else {
-        $product = $wrapper->{$field_name}->get(0)->value();
+        $product = commerce_product_reference_default_product($wrapper->{$field_name}->value());
 
         // If the product is disabled, attempt to find one that is active and
         // use that as the default product instead.
@@ -1390,6 +1390,23 @@ function commerce_product_reference_preprocess_entity(&$variables) {
 }
 
 /**
+ * Returns the default product from the array of Commerce product entities,
+ * providing option for other modules to alter its value.
+ *
+ * @param array $products
+ *   Array of Commerce product entities.
+ *
+ * @return
+ *   The fully loaded Commerce product entity.
+ */
+function commerce_product_reference_default_product($products) {
+  $default_product_delta = reset(array_keys($products));
+  drupal_alter('commerce_product_reference_default_product', $default_product_delta, $products);
+
+  return $products[$default_product_delta];
+}
+
+/**
  * Exception thrown when the referenced product display formatter goes into a
  * potentially infinite loop.
  */
-- 
1.7.2.5

