You are here

function uc_weightquote_form_alter in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_weightquote/uc_weightquote.module \uc_weightquote_form_alter()
  2. 7.3 shipping/uc_weightquote/uc_weightquote.module \uc_weightquote_form_alter()

Implementation of hook_form_alter().

Add a form element for the shipping rate of a product.

See also

uc_product_form

File

shipping/uc_weightquote/uc_weightquote.module, line 55
Shipping quote module that defines a shipping rate for each product based on weight.

Code

function uc_weightquote_form_alter($form_id, &$form) {
  $node = $form['#node'];
  $product_types = module_invoke_all('product_types');
  if (is_object($node) && $form_id == $node->type . '_node_form' && isset($form['base']['dimensions']) && in_array($node->type, $product_types)) {
    $result = db_query("SELECT mid, title, unit_rate FROM {uc_weightquote_methods}");
    if (db_num_rows($result)) {
      $sign_flag = variable_get('uc_sign_after_amount', FALSE);
      $currency_sign = variable_get('uc_currency_sign', '$');
      $enabled = variable_get('uc_quote_enabled', array());
      $weight = variable_get('uc_quote_method_weight', array());
      $form['shipping']['weightquote'] = array(
        '#type' => 'fieldset',
        '#title' => t('Weight quote shipping rates'),
        '#description' => t('Override the default shipping rate per !unit for each weight quote shipping method here. Enter -1 to revert to the default value.', array(
          '!unit' => variable_get('uc_weight_unit', 'lb'),
        )),
        '#tree' => true,
        '#collapsible' => true,
        '#collapsed' => false,
        '#weight' => 0,
      );
      while ($method = db_fetch_object($result)) {
        $form['shipping']['weightquote'][$method->mid] = array(
          '#type' => 'textfield',
          '#title' => $method->title,
          '#default_value' => $form['#node']->weightquote[$method->mid],
          '#description' => t('Default rate per !unit: %price', array(
            '!unit' => variable_get('uc_weight_unit', 'lb'),
            '%price' => uc_currency_format($method->unit_rate),
          )),
          '#size' => 16,
          '#field_prefix' => $sign_flag ? '' : $currency_sign,
          '#field_suffix' => t('!sign per !unit', array(
            '!sign' => $sign_flag ? $currency_sign : '',
            '!unit' => variable_get('uc_weight_unit', 'lb'),
          )),
          '#weight' => $weight['weightquote_' . $method->mid],
        );
      }
    }
  }
}