You are here

function uc_weightquote_form_alter in Ubercart 7.3

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

Implements hook_form_alter().

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

File

shipping/uc_weightquote/uc_weightquote.module, line 49
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)) {
    $weight = variable_get('uc_quote_method_weight', array());
    $result = db_query("SELECT mid, title, product_rate FROM {uc_weightquote_methods}");
    foreach ($result as $method) {

      // Ensure the default weight is set.
      $weight += array(
        'weightquote_' . $method->mid => 0,
      );
      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' => 'uc_price',
        '#title' => check_plain($method->title),
        '#default_value' => isset($form['#node']->weightquote[$method->mid]) ? $form['#node']->weightquote[$method->mid] : '',
        '#description' => t('Default rate: %price per @unit', array(
          '%price' => uc_currency_format($method->product_rate),
          '@unit' => variable_get('uc_weight_unit', 'lb'),
        )),
        '#field_suffix' => t('per @unit', array(
          '@unit' => variable_get('uc_weight_unit', 'lb'),
        )),
        '#weight' => $weight['weightquote_' . $method->mid],
        '#empty_zero' => FALSE,
      );
    }
  }
}