You are here

function uc_weightquote_form_alter in Ubercart 6.2

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

Implements hook_form_alter().

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

File

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

Code

function uc_weightquote_form_alter(&$form, $form_state, $form_id) {
  if (uc_product_is_product_form($form)) {
    $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());
    $result = db_query("SELECT mid, title, product_rate FROM {uc_weightquote_methods}");
    $context = array(
      'revision' => 'formatted',
      'location' => 'shipping-weightquote-method-node-form',
      'subject' => array(),
    );
    while ($method = db_fetch_object($result)) {
      $context['subject']['weightquote_method'] = $method;
      if (!isset($form['shipping']['weightquote'])) {
        $form['shipping']['weightquote'] = array(
          '#type' => 'fieldset',
          '#title' => t('Shipping rates by weight'),
          '#description' => t("Overrides the default shipping rate per product for each flat rate shipping method. Leave field empty to use the method's default value."),
          '#tree' => TRUE,
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
          '#weight' => 0,
        );
      }
      $form['shipping']['weightquote'][$method->mid] = array(
        '#type' => 'textfield',
        '#title' => $method->title,
        '#default_value' => isset($form['#node']->weightquote[$method->mid]) ? $form['#node']->weightquote[$method->mid] : '',
        '#description' => t('Default rate: %price/%unit', array(
          '%price' => uc_price($method->product_rate, $context),
          '%unit' => variable_get('uc_weight_unit', 'lb'),
        )),
        '#size' => 16,
        '#field_prefix' => $sign_flag ? '' : $currency_sign,
        '#field_suffix' => t('@sign/@unit', array(
          '@sign' => $sign_flag ? $currency_sign : '',
          '@unit' => variable_get('uc_weight_unit', 'lb'),
        )),
        '#weight' => $weight['weightquote_' . $method->mid],
      );
    }
  }
}