Index: uc_aac.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/uc_aac/uc_aac.module,v
retrieving revision 1.10.2.4
diff -u -p -r1.10.2.4 uc_aac.module
--- uc_aac.module	13 Nov 2009 01:02:57 -0000	1.10.2.4
+++ uc_aac.module	23 Nov 2009 17:00:17 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: uc_aac.module,v 1.10.2.4 2009/11/13 01:02:57 antoinesolutions Exp $
+// $Id: uc_aac.module,v 1.10.2.1 2009/11/10 17:25:00 antoinesolutions Exp $
 
 /**
  * @file
@@ -47,47 +47,47 @@ function uc_aac_form_alter(&$form, $form
       drupal_add_js("$('.attributes').find('[@name^=attributes]:first').each(function(i) { uc_aac_calculate(this); });", 'inline', 'footer');
     }
 
-    // If reprice is enabled and price adjustments are to be displayed
-    if (variable_get('uc_aac_reprice', 1) == 1 && variable_get('uc_attribute_option_price_format', 'adjustment') == 'adjustment') {
-      // Alter prices if we were called with POSTed attributes.
-      if (isset($form['#parameters'][2]->attributes)) {
-        $nid = $form['nid']['#value'];
-        $product =& $form['#parameters'][2];
-        $form['qty']['#default_value'] = $product->default_qty;
-        $context = array(
-          'revision' => 'formatted',
-          'type' => 'product',
-          'class' => array(
-            'product',
-          ),
-          'subject' => array(
-            'node' => $product,
-          ),
-        );
-
-        foreach (element_children($form['attributes']) as $aid) {
-          // Reset options for each $aid
-          $options = array();
-
-          if ($form['attributes'][$aid]['#type'] == 'select' || $form['attributes'][$aid]['#type'] == 'radios') {
-            $selected_oid = $product->attributes[$aid]->default_option;
-            $form['attributes'][$aid]['#default_value'] = $selected_oid;
+    // If the product has attributes.
+    if (isset($form['#parameters'][2]->attributes)) {
+      $nid = $form['nid']['#value'];
+      $product =& $form['#parameters'][2];
+      $form['qty']['#default_value'] = $product->default_qty;
+      $context = array(
+        'revision' => 'formatted',
+        'type' => 'product',
+        'class' => array(
+          'product',
+        ),
+        'subject' => array(
+          'node' => $product,
+        ),
+      );
+
+      foreach (element_children($form['attributes']) as $aid) {
+        // Reset options for each $aid
+        $options = array();
 
-            // Rebuild option text
-            foreach ($product->attributes[$aid]->options as $option) {
-              $price = $option->price - $product->attributes[$aid]->options[$selected_oid]->price;
-              $options[$option->oid] = $option->name . ($price != 0 ? ', '. ($price > 0 ? '+' : '') . uc_price($price, $context) : '');
-            }
-          }
-          elseif ($form['attributes'][$aid]['#type'] == 'checkboxes') {
-            // TODO: Find out why the default option is not always an array.
-            $selected_oid = $product->attributes[$aid]->default_option;
-            if (!is_array($selected_oid)) {
-              $selected_oid = array();
-            }
-            $form['attributes'][$aid]['#default_value'] = $selected_oid;
+        if (isset($_POST['attributes'][$aid])) {
+          $selected_oid = $_POST['attributes'][$aid];
+        }
+        else {
+          $selected_oid = $product->attributes[$aid]->default_option;
+        }
+        // TODO: Find out why the default checkbox option is not always an array.
+        if ($form['attributes'][$aid]['#type'] == 'checkboxes' && !is_array($selected_oid)) {
+          $selected_oid = array();
+        }
+        // TODO: Find out why the default text option is not always an empty string.
+        if ($form['attributes'][$aid]['#type'] == 'textfield') {
+          $selected_oid = ($selected_oid === '0' ? '' : $selected_oid);
+        }
+        $form['attributes'][$aid]['#default_value'] = $selected_oid;
 
-            foreach ($product->attributes[$aid]->options as $option) {
+        // If reprice is enabled and price adjustments are to be displayed
+        if (variable_get('uc_aac_reprice', 1) == 1 && variable_get('uc_attribute_option_price_format', 'adjustment') == 'adjustment'
+            || (count($product->attributes) > 1 && variable_get('uc_attribute_option_price_format', 'adjustment') == 'total')) {
+          foreach ($product->attributes[$aid]->options as $option) {
+            if ($form['attributes'][$aid]['#type'] == 'checkboxes') {
               // Negate selected options
               if (in_array($option->oid, $selected_oid)) {
                 $price = -$option->price;
@@ -95,26 +95,29 @@ function uc_aac_form_alter(&$form, $form
               else {
                 $price = $option->price;
               }
+            }
+            else {
+              $price = $option->price - $product->attributes[$aid]->options[$selected_oid]->price;
+            }
 
-              // Rebuild option text
-              $oid_text = '';
-              if ($price != 0) {
-                $oid_text = ', ';
-                if ($price > 0) {
-                  $oid_text .= '+'. uc_price($price, $context);
-                }
-                else {
-                  $oid_text .= '('. uc_price($price, $context) .')';
-                }
+            // Rebuild option text
+            $oid_text = '';
+            if ($price != 0) {
+              $oid_text = ', ';
+              if ($price > 0) {
+                $oid_text .= '+'. uc_price($price, $context);
+              }
+              else {
+                $oid_text .= '('. uc_price($price, $context) .')';
               }
-              $options[$option->oid] = $option->name . $oid_text;
             }
+            $options[$option->oid] = $option->name . $oid_text;
           }
+        }
 
-          // If options have been reconfigured
-          if (!empty($options)) {
-            $form['attributes'][$aid]['#options'] = $options;
-          }
+        // If options have been reconfigured
+        if (!empty($options)) {
+          $form['attributes'][$aid]['#options'] = $options;
         }
       }
     }
