You are here

function uc_discounts_form_uc_cart_view_form_alter in Ubercart Discounts (Alternative) 7.2

Implements hook_form_FORM_ID_alter().

Alters ubercart shopping cart view form adding discount subtotals.

See also

uc_cart_view_form()

File

uc_discounts/uc_discounts.module, line 464

Code

function uc_discounts_form_uc_cart_view_form_alter(&$form, &$form_state) {
  $enabled = variable_get('uc_cap_uc_discounts_enabled', TRUE);
  if ($enabled) {
    $total_key = 0;
    $order = FALSE;
    $cart_id = uc_cart_get_id(FALSE);
    foreach ($form['items'] as $key => $item) {
      if (isset($item['total']['#prefix']) && strstr($item['total']['#prefix'], 'subtotal')) {
        $total_key = $key;
        break;
      }
    }
    foreach ($form['items'] as $key => $item) {
      if (isset($item['#entity']->order->order_id)) {
        $order = $item['#entity']->order;
        break;
      }
    }
    if ($order && $total_key && $cart_id) {
      $order->products = uc_cart_get_contents($cart_id);
      $discount_amount = uc_discounts_get_discount_amount_for_order($order);
      if ($discount_amount) {
        $form['items'][] = array(
          'total' => array(
            '#theme' => 'uc_price',
            '#prefix' => '<span id="discount-amount-title">' . t('Total discount:') . '</span>',
            '#price' => -$discount_amount,
            '#cell_attributes' => array(
              'colspan' => 'full',
              'class' => array(
                'subtotal',
              ),
            ),
          ),
        );
        $form['items'][] = array(
          'total' => array(
            '#theme' => 'uc_price',
            '#prefix' => '<span id="discount-subtotal-title">' . t('Subtotal with discount:') . '</span>',
            '#price' => $form['items'][$total_key]['total']['#price'] - $discount_amount,
            '#cell_attributes' => array(
              'colspan' => 'full',
              'class' => array(
                'subtotal',
              ),
            ),
          ),
        );
      }
    }
  }
}