You are here

function uc_flatrate_form_alter in Ubercart 5

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

Implementation of hook_form_alter().

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

File

shipping/uc_flatrate/uc_flatrate.module, line 53
Shipping quote module that defines a flat shipping rate for each product.

Code

function uc_flatrate_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, product_rate FROM {uc_flatrate_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']['flatrate'] = array(
        '#type' => 'fieldset',
        '#title' => t('Flat shipping rates'),
        '#description' => t('Override the default shipping rate per product for each flat rate shipping method here. Enter -1 to revert to the default value.'),
        '#tree' => true,
        '#collapsible' => true,
        '#collapsed' => false,
        '#weight' => 0,
      );
      while ($method = db_fetch_object($result)) {
        $form['shipping']['flatrate'][$method->mid] = array(
          '#type' => 'textfield',
          '#title' => $method->title,
          '#default_value' => $form['#node']->flatrate[$method->mid],
          '#description' => t('Default rate: %price', array(
            '%price' => uc_currency_format($method->product_rate),
          )),
          '#size' => 16,
          '#field_prefix' => $sign_flag ? '' : $currency_sign,
          '#field_suffix' => $sign_flag ? $currency_sign : '',
          '#weight' => $weight['flatrate_' . $method->mid],
        );
      }
    }
  }
}