You are here

function _uc_attribute_alter_form in Ubercart 5

Same name and namespace in other branches
  1. 8.4 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
  2. 6.2 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()
  3. 7.3 uc_attribute/uc_attribute.module \_uc_attribute_alter_form()

Helper function for uc_attribute_form_alter()

1 call to _uc_attribute_alter_form()
uc_attribute_form_alter in uc_attribute/uc_attribute.module
Implementation of hook_form_alter().

File

uc_attribute/uc_attribute.module, line 1509

Code

function _uc_attribute_alter_form($product) {
  $nid = intval($product['nid']['#value']);
  $qty = intval($product['qty']['#value']);
  if (!$qty) {
    $qty = 1;
  }

  // If we were passed an actual node object...
  if ($node = node_load($nid)) {

    // Load all product attributes for the given nid.
    $result = db_query("SELECT * FROM {uc_product_attributes} WHERE nid = %d ORDER BY ordering", $nid);
    $priced_attributes = uc_attribute_priced_attributes($nid);

    // Loop through each product attribute and generate its form element.
    while ($relation = db_fetch_object($result)) {
      $attribute = uc_attribute_load($relation->aid, $nid, 'product');

      // Build the attribute's options array.
      $options = array();
      foreach ($attribute->options as $option) {
        switch (variable_get('uc_attribute_option_price_format', 'adjustment')) {
          case 'total':
            $display_price = in_array($relation->aid, $priced_attributes) ? ', ' . uc_currency_format(($node->sell_price + $option->price) * $qty) : '';
            if (count($priced_attributes) == 1) {
              break;
            }
          case 'adjustment':
            $display_price = $option->price != 0 ? ', ' . ($option->price > 0 ? '+' : '') . uc_currency_format($option->price * $qty) : '';
            break;
          case 'none':
          default:
            $display_price = '';
            break;
        }
        $options[$option->oid] = $option->name . $display_price;
      }
      if (count($attribute->options) && $attribute->display > 0) {
        if ($attribute->required) {
          if ($attribute->display == 1) {
            $options = array(
              '' => t('Please select'),
            ) + $options;
          }
          unset($relation->default_option);
        }
        $product['attributes'][$attribute->aid] = array(
          '#type' => $attribute->display == 1 ? 'select' : 'radios',
          '#title' => $attribute->name,
          '#description' => check_markup($attribute->description),
          '#default_value' => $relation->default_option,
          '#options' => $options,
          '#required' => $attribute->required,
        );
      }
      else {
        $product['attributes'][$attribute->aid] = array(
          '#type' => 'textfield',
          '#title' => $attribute->name,
          '#description' => check_markup($attribute->description),
          '#default_value' => $attribute->required == FALSE ? $attribute->options[$relation->default_option]->name : '',
          '#required' => $attribute->required,
        );
      }
      $product['attributes']['#theme'] = 'uc_attribute_add_to_cart';
    }
  }
  return $product;
}